1 import javax.jms.Connection;
2 import javax.jms.ConnectionFactory;
3 import javax.jms.Destination;
4 import javax.jms.MessageConsumer;
5 import javax.jms.Session;
6 import javax.jms.TextMessage;
7
8 import org.apache.activemq.ActiveMQConnection;
9 import org.apache.activemq.ActiveMQConnectionFactory;
10
11 public class QueueReceive{
12 public static void main(String[] args) {
13
14 // ConnectionFactory :連接工廠(chǎng),JMS 用它創(chuàng)建連接
15 ConnectionFactory connectionFactory;
16 // Connection :JMS 客戶(hù)端到JMS Provider 的連接
17 Connection connection = null;
18 // Session: 一個(gè)發(fā)送或接收消息的線(xiàn)程
19 Session session;
20 // Destination :消息的目的地;消息發(fā)送給誰(shuí).
21 Destination destination;
22 // 消費(fèi)者,消息接收者
23 MessageConsumer consumer;
24
25 connectionFactory = new ActiveMQConnectionFactory(
26 ActiveMQConnection.DEFAULT_USER,
27 ActiveMQConnection.DEFAULT_PASSWORD,
28 "tcp://localhost:61616");
29 try {
30 // 構(gòu)造從工廠(chǎng)得到連接對(duì)象
31 connection = connectionFactory.createConnection();
32 // 啟動(dòng)
33 connection.start();
34 // 獲取操作連接
35 session = connection.createSession(Boolean.FALSE,
36 Session.AUTO_ACKNOWLEDGE);
37 // 獲取session注意參數(shù)是一個(gè)服務(wù)器的queue,須在在A(yíng)ctiveMq的console配置
38 destination = session.createQueue("queue1");
39 consumer = session.createConsumer(destination);
40 while (true) {
41 TextMessage message = (TextMessage) consumer.receive(1000);
42 if (null != message) {
43 System.out.println("收到消息" + message.getText());
44
45 } else {
46 break;
47 }
48
49 }
50
51 } catch (Exception e) {
52 e.printStackTrace();
53 } finally {
54 try {
55 if (null != connection)
56 connection.close();
57 } catch (Throwable ignore) {
58 }
59 }
60
61 }
62
2 import javax.jms.ConnectionFactory;
3 import javax.jms.Destination;
4 import javax.jms.MessageConsumer;
5 import javax.jms.Session;
6 import javax.jms.TextMessage;
7
8 import org.apache.activemq.ActiveMQConnection;
9 import org.apache.activemq.ActiveMQConnectionFactory;
10
11 public class QueueReceive{
12 public static void main(String[] args) {
13
14 // ConnectionFactory :連接工廠(chǎng),JMS 用它創(chuàng)建連接
15 ConnectionFactory connectionFactory;
16 // Connection :JMS 客戶(hù)端到JMS Provider 的連接
17 Connection connection = null;
18 // Session: 一個(gè)發(fā)送或接收消息的線(xiàn)程
19 Session session;
20 // Destination :消息的目的地;消息發(fā)送給誰(shuí).
21 Destination destination;
22 // 消費(fèi)者,消息接收者
23 MessageConsumer consumer;
24
25 connectionFactory = new ActiveMQConnectionFactory(
26 ActiveMQConnection.DEFAULT_USER,
27 ActiveMQConnection.DEFAULT_PASSWORD,
28 "tcp://localhost:61616");
29 try {
30 // 構(gòu)造從工廠(chǎng)得到連接對(duì)象
31 connection = connectionFactory.createConnection();
32 // 啟動(dòng)
33 connection.start();
34 // 獲取操作連接
35 session = connection.createSession(Boolean.FALSE,
36 Session.AUTO_ACKNOWLEDGE);
37 // 獲取session注意參數(shù)是一個(gè)服務(wù)器的queue,須在在A(yíng)ctiveMq的console配置
38 destination = session.createQueue("queue1");
39 consumer = session.createConsumer(destination);
40 while (true) {
41 TextMessage message = (TextMessage) consumer.receive(1000);
42 if (null != message) {
43 System.out.println("收到消息" + message.getText());
44
45 } else {
46 break;
47 }
48
49 }
50
51 } catch (Exception e) {
52 e.printStackTrace();
53 } finally {
54 try {
55 if (null != connection)
56 connection.close();
57 } catch (Throwable ignore) {
58 }
59 }
60
61 }
62
1 import javax.jms.Connection;
2 import javax.jms.ConnectionFactory;
3 import javax.jms.DeliveryMode;
4 import javax.jms.Destination;
5 import javax.jms.MessageProducer;
6 import javax.jms.Session;
7 import javax.jms.TextMessage;
8
9 import org.apache.activemq.ActiveMQConnection;
10 import org.apache.activemq.ActiveMQConnectionFactory;
11
12 public class QueueSend {
13 private static final int SEND_NUMBER = 5;
14
15 public static void main(String[] args) {
16 // ConnectionFactory :連接工廠(chǎng),JMS 用它創(chuàng)建連接
17 ConnectionFactory connectionFactory;
18 // Connection :JMS 客戶(hù)端到JMS Provider 的連接
19 Connection connection = null;
20 // Session: 一個(gè)發(fā)送或接收消息的線(xiàn)程
21 Session session;
22 // Destination :消息的目的地;消息發(fā)送給誰(shuí).
23 Destination destination;
24 // MessageProducer:消息發(fā)送者
25 MessageProducer producer;
26 // TextMessage message;
27 // 構(gòu)造ConnectionFactory實(shí)例對(duì)象,此處采用ActiveMq的實(shí)現(xiàn)jar
28
29 connectionFactory = new ActiveMQConnectionFactory(
30 ActiveMQConnection.DEFAULT_USER,
31 ActiveMQConnection.DEFAULT_PASSWORD,
32 "tcp://localhost:61616");
33
34 try {
35 // 構(gòu)造從工廠(chǎng)得到連接對(duì)象
36 connection = connectionFactory.createConnection();
37 // 啟動(dòng)
38 connection.start();
39 // 獲取操作連接
40 session = connection.createSession(Boolean.TRUE,
41 Session.AUTO_ACKNOWLEDGE);
42 // queue1需要在admin界面創(chuàng)建
43 destination = session.createQueue("queue1");
44 // 得到消息生成者
45
46 producer = session.createProducer(destination);
47 // 設(shè)置不持久化,此處學(xué)習(xí),實(shí)際根據(jù)項(xiàng)目決定
48 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
49 // 構(gòu)造消息,此處寫(xiě)死,項(xiàng)目就是參數(shù),或者方法獲取
50 sendMessage(session, producer);
51 session.commit();
52
53 } catch (Exception e) {
54 e.printStackTrace();
55 } finally {
56 try {
57 if (null != connection)
58 connection.close();
59 } catch (Throwable ignore) {
60 }
61 }
62
63 }
64
65 public static void sendMessage(Session session, MessageProducer producer)
66 throws Exception {
67 for (int i = 1; i <=SEND_NUMBER; i++) {
68 TextMessage message = session
69 .createTextMessage("ActiveMq 發(fā)送的消息" + i);
70 // 發(fā)送消息到目的地方
71 System.out.println("發(fā)送消息:" + i+"成功");
72 producer.send(message);
73 }
74 }
75 }
}2 import javax.jms.ConnectionFactory;
3 import javax.jms.DeliveryMode;
4 import javax.jms.Destination;
5 import javax.jms.MessageProducer;
6 import javax.jms.Session;
7 import javax.jms.TextMessage;
8
9 import org.apache.activemq.ActiveMQConnection;
10 import org.apache.activemq.ActiveMQConnectionFactory;
11
12 public class QueueSend {
13 private static final int SEND_NUMBER = 5;
14
15 public static void main(String[] args) {
16 // ConnectionFactory :連接工廠(chǎng),JMS 用它創(chuàng)建連接
17 ConnectionFactory connectionFactory;
18 // Connection :JMS 客戶(hù)端到JMS Provider 的連接
19 Connection connection = null;
20 // Session: 一個(gè)發(fā)送或接收消息的線(xiàn)程
21 Session session;
22 // Destination :消息的目的地;消息發(fā)送給誰(shuí).
23 Destination destination;
24 // MessageProducer:消息發(fā)送者
25 MessageProducer producer;
26 // TextMessage message;
27 // 構(gòu)造ConnectionFactory實(shí)例對(duì)象,此處采用ActiveMq的實(shí)現(xiàn)jar
28
29 connectionFactory = new ActiveMQConnectionFactory(
30 ActiveMQConnection.DEFAULT_USER,
31 ActiveMQConnection.DEFAULT_PASSWORD,
32 "tcp://localhost:61616");
33
34 try {
35 // 構(gòu)造從工廠(chǎng)得到連接對(duì)象
36 connection = connectionFactory.createConnection();
37 // 啟動(dòng)
38 connection.start();
39 // 獲取操作連接
40 session = connection.createSession(Boolean.TRUE,
41 Session.AUTO_ACKNOWLEDGE);
42 // queue1需要在admin界面創(chuàng)建
43 destination = session.createQueue("queue1");
44 // 得到消息生成者
45
46 producer = session.createProducer(destination);
47 // 設(shè)置不持久化,此處學(xué)習(xí),實(shí)際根據(jù)項(xiàng)目決定
48 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
49 // 構(gòu)造消息,此處寫(xiě)死,項(xiàng)目就是參數(shù),或者方法獲取
50 sendMessage(session, producer);
51 session.commit();
52
53 } catch (Exception e) {
54 e.printStackTrace();
55 } finally {
56 try {
57 if (null != connection)
58 connection.close();
59 } catch (Throwable ignore) {
60 }
61 }
62
63 }
64
65 public static void sendMessage(Session session, MessageProducer producer)
66 throws Exception {
67 for (int i = 1; i <=SEND_NUMBER; i++) {
68 TextMessage message = session
69 .createTextMessage("ActiveMq 發(fā)送的消息" + i);
70 // 發(fā)送消息到目的地方
71 System.out.println("發(fā)送消息:" + i+"成功");
72 producer.send(message);
73 }
74 }
75 }