ActiveMQ学习笔记(一)——HelloWorld

HelloProducer


// 生产者的主要流程  

Connection conn = null;

//1.初始化连接工厂

ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://172.16.41.156:61616");

// 2.创建Connection  

conn = factory.createConnection();  

 // 3.打开连接

conn.start();

// 4.创建session,是否支持事务

Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);  

 // 5.创建消息目标  

Destination queue = session.createQueue("queue_hello");  

 //6.创建生产者  

MessageProducer producer = session.createProducer(queue);

//7.配置消息是否持久化  

/*  DeliverMode有2种方式: 

 *  

     public interface DeliveryMode { 

                static final int NON_PERSISTENT = 1;//不持久化:服务器重启之后,消息销毁 

                static final int PERSISTENT = 2;//持久化:服务器重启之后,该消息仍存在 

      } 

 */  

producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);  

 //8.初始化要发送的消息  

TextMessage message = session.createTextMessage(" Hello World ! from activemq"); 

//9.发送消息

producer.send(message);

//10.关闭连接

conn.close();


HelloConsumer implements TextListener

@Override

public void onMessage(Message message) {

        //如果消息是TextMessage  

        if (message instanceof TextMessage) {  

            //强制转换一下  

            TextMessage txtMsg = (TextMessage) message;  

            try {  

                //输出接收到的消息  

                System.out.println("我爱罗接收到漩涡鸣人的信息 : " + txtMsg.getText());  

            } catch (JMSException e) {  

                e.printStackTrace();  

            }  

        }          

}


public void receive() {  

        // 消费者的主要流程  

        Connection connection = null;  

        try {  

            // 1.初始化connection工厂  

            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://172.16.41.156:61616");  

            // 2.创建Connection  

            connection = connectionFactory.createConnection();  

            // 3.打开连接  

            connection.start();  

            // 4.创建session  

            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 

            // 5.创建消息目标  

            Destination queue= session.createQueue("queue_hello");  

            //6.创建消费者  

            MessageConsumer consumer = session.createConsumer(queue);          

            //7.配置监听  

            consumer.setMessageListener(this);  

        } catch (JMSException e) {  

            e.printStackTrace();  

        }  

 }  


public static void main(String[] args) {

        new HelloConsumer().receive();

}


ActiveMq如果与Spring配置

  //有对应所需功能api,其余配置只需要在xml文件里面配置

  private JmsTemplate jmsTemplate;   

  public JmsTemplate getJmsTemplate() {  

     return jmsTemplate;  

  }  

  public void setJmsTemplate(JmsTemplate jmsTemplate) {  

     this.jmsTemplate = jmsTemplate;  

  }  


spring配置内容

http://blog.csdn.net/yuguiyang1990/article/details/12714291

http://blog.csdn.net/yuguiyang1990/article/details/12714739


以上转载

http://blog.csdn.net/yuguiyang1990/article/details/12090505


评论

© dzxlovelar | Powered by LOFTER