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

ActiveMQ使用

时间:2014-05-26 00:41:20      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   c   code   

1.下载ActiveMQ

去官方网站下载:http://activemq.apache.org/

2.创建Eclipse项目并运行

创建project:ActiveMQ-5.5,并导入apache-activemq-5.5.1\lib目录下需要用到的jar文件,项目结构如下图所示:

bubuko.com,布布扣

先启动activeMQ.BAT;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.mzj.jmsActiveMQTest2;
 
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
 
import org.apache.activemq.ActiveMQConnectionFactory;
/**
 * 消息——生产者
 */
public class Producer {
 
    public static void main(String[] args) throws JMSException {
 
        String jmsProviderAddress = "tcp://localhost:61616";// 地址
 
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                jmsProviderAddress);// 连接器
 
        Connection connection = connectionFactory.createConnection();// 创建连接
 
        Session session = connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);// 打开会话
 
        Destination dest = session.createQueue("demoQueue");// 消息目的地
 
        MessageProducer producer = session.createProducer(dest);// 消息发送者
 
        Message message = session.createTextMessage("hello world");// 消息
 
        producer.send(message);// 发送
 
        producer.close();// 关闭
        session.close();
        connection.close();
 
    }
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.mzj.jmsActiveMQTest2;
 
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.Session;
import javax.jms.TextMessage;
 
import org.apache.activemq.ActiveMQConnectionFactory;
/**
 * 消息——使用者
 */
public class Consumer {
    public static void main(String[] args) throws JMSException {
 
        String jmsProviderAddress = "tcp://localhost:61616";// 地址
 
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                jmsProviderAddress);// 连接器
 
        Connection connection = connectionFactory.createConnection();// 创建连接
 
        Session session = connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);// 打开会话
 
        String destinationName = "demoQueue";
 
        Destination dest = session.createQueue(destinationName);// 消息目的地
 
        MessageConsumer consumer = session.createConsumer(dest);
 
        connection.start();
 
        Message message = consumer.receive();
 
        TextMessage textMessage = (TextMessage) message;
 
        String text = textMessage.getText();
 
        System.out.println("从ActiveMQ取回一条消息: " + text);
 
        consumer.close();
        session.close();
        connection.close();
 
    }
 
}

  

ActiveMQ使用,布布扣,bubuko.com

ActiveMQ使用

标签:des   style   class   blog   c   code   

原文地址:http://www.cnblogs.com/muzhongjiang/p/3750033.html

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