标签:des style class blog c code
去官方网站下载:http://activemq.apache.org/
创建project:ActiveMQ-5.5,并导入apache-activemq-5.5.1\lib目录下需要用到的jar文件,项目结构如下图所示:
先启动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 { 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 { 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(); } } |
标签:des style class blog c code
原文地址:http://www.cnblogs.com/muzhongjiang/p/3750033.html