码迷,mamicode.com
首页 > 其他好文 > 详细

activemq p2p方式

时间:2014-11-06 16:53:29      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   os   使用   java   

package ch02.chat;



import java.io.Serializable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.Session;
/*本工具封装了*/


import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSQueue {
    TopicConnectionFactory connectionFactory;
    // Connection :JMS 客户端到JMS Provider 的连接
    TopicConnection connection = null;
    //用来发布的会话
    
    //TopicSession proSession = null;
    //2一个订阅会话
   //TopicSession conSession = null;
          Session  session=null;
    
    //主题发布者
    MessageProducer producer=null;
    //主题
    MessageConsumer consumer=null;
    
    
    // Destination :消息的目的地;消息发送给谁.
    Destination destination=null;
    // MessageProducer:消息发送者
    
    //默认构造函数,默认的连接activemq,可以写多个构造函数
    public JMSQueue() 
    { 
        connectionFactory =  new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
        try {
            connection= connectionFactory.createTopicConnection();
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            connection.start();
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    public JMSQueue(String user,String name) 
    { 
        connectionFactory =  new ActiveMQConnectionFactory(
                user,
                name,
                "tcp://localhost:61616");
        try {
            connection= connectionFactory.createTopicConnection();
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            connection.start();
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    
    
    
    
    //设计session类型
       public void setSession() throws JMSException
       {
           session= connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
          
       }
         //设置为原子类型
       public void setAtomicSession() throws JMSException
       {
           session= connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
           
       }
    
    //此处先固定消息为String类型
    public void writeMessage(String t,String message,int priority )
    {
        try {
            
            producer=session.createProducer(session.createQueue(t));
            
            //使用message构造TextMessage 
            TextMessage text=session.createTextMessage();
            text.setJMSPriority(priority);
            text.setText(message);
            producer.send(text);
            
            
            
            
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //创建发布会话应该是可以配置的,此处先固定
        
        
    }
    
    public void writeMessage(String t,Object o)
    {
        try {

            
            producer=session.createProducer(session.createQueue(t));
            
            //使用message构造TextMessage 
            ObjectMessage text=session.createObjectMessage();
            text.setObject((Serializable) o);
            producer.send(text);
            
            
            
            
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //创建发布会话应该是可以配置的,此处先固定
        
        
    }
        
    //使用某个Message监听器来监听某个Topic
    public void receiveMsg(String c,MessageListener ml)
    {
        try {
        
            Queue t=session.createQueue(c);
            consumer=session.createConsumer(t);
            //设置过来的监视器
            consumer.setMessageListener(ml);
            
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        
        
    }
    public Message receiveMsg(String c)
    {
        try {
            
            
            consumer=session.createConsumer(session.createQueue(c));
            //设置过来的监视器
            Message message=consumer.receive();
            return message;
            
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    //同步接收信息
    
    public void commit() throws JMSException
    {
        session.commit();
    }
    public void rollback() throws JMSException
    {
        session.rollback();
    }
    public void close() throws JMSException 
    {
        if(connection!=null) connection.close();
        if(session!=null) session.close();
        if(producer!=null) session.close();
        if(consumer!=null) consumer.close();
        
        
        
        
    }
    

}

 

activemq p2p方式

标签:des   style   blog   io   color   ar   os   使用   java   

原文地址:http://www.cnblogs.com/hansongjiang/p/4078671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!