娑堣垂鑰咃紝娑堟伅鎺ユ敹鑰?/span>
23 MessageConsumer consumer;
24
25 connectionFactory = new ActiveMQConnectionFactory(
26 ActiveMQConnection.DEFAULT_USER,
27 ActiveMQConnection.DEFAULT_PASSWORD,
28 "tcp://localhost:61616");
29 try {
30 // 鏋勯犱粠宸ュ巶寰楀埌榪炴帴瀵硅薄
31 connection = connectionFactory.createConnection();
32 // 鍚姩
33 connection.start();
34 // 鑾峰彇鎿嶄綔榪炴帴
35 session = connection.createSession(Boolean.FALSE,
36 Session.AUTO_ACKNOWLEDGE);
37 // 鑾峰彇session娉ㄦ剰鍙傛暟鏄竴涓湇鍔″櫒鐨剄ueue錛岄』鍦ㄥ湪ActiveMq鐨刢onsole閰嶇疆
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 錛氳繛鎺ュ伐鍘傦紝JMS 鐢ㄥ畠鍒涘緩榪炴帴
17 ConnectionFactory connectionFactory;
18 // Connection 錛欽MS 瀹㈡埛绔埌JMS Provider 鐨勮繛鎺?/span>
19 Connection connection = null;
20 // Session錛?nbsp;涓涓彂閫佹垨鎺ユ敹娑堟伅鐨勭嚎紼?/span>
21 Session session;
22 // Destination 錛氭秷鎭殑鐩殑鍦?娑堟伅鍙戦佺粰璋?
23 Destination destination;
24 // MessageProducer錛氭秷鎭彂閫佽?/span>
25 MessageProducer producer;
26 // TextMessage message;
27 // 鏋勯燙onnectionFactory瀹炰緥瀵硅薄錛屾澶勯噰鐢ˋctiveMq鐨勫疄鐜癹ar
28
29 connectionFactory = new ActiveMQConnectionFactory(
30 ActiveMQConnection.DEFAULT_USER,
31 ActiveMQConnection.DEFAULT_PASSWORD,
32 "tcp://localhost:61616");
33
34 try {
35 // 鏋勯犱粠宸ュ巶寰楀埌榪炴帴瀵硅薄
36 connection = connectionFactory.createConnection();
37 // 鍚姩
38 connection.start();
39 // 鑾峰彇鎿嶄綔榪炴帴
40 session = connection.createSession(Boolean.TRUE,
41 Session.AUTO_ACKNOWLEDGE);
42 // queue1闇瑕佸湪admin鐣岄潰鍒涘緩
43 destination = session.createQueue("queue1");
44 // 寰楀埌娑堟伅鐢熸垚鑰?/span>
45
46 producer = session.createProducer(destination);
47 // 璁劇疆涓嶆寔涔呭寲錛屾澶勫涔狅紝瀹為檯鏍規嵁欏圭洰鍐沖畾
48 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
49 // 鏋勯犳秷鎭紝姝ゅ鍐欐錛岄」鐩氨鏄弬鏁幫紝鎴栬呮柟娉曡幏鍙?/span>
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 鍙戦佺殑娑堟伅" + i);
70 // 鍙戦佹秷鎭埌鐩殑鍦版柟
71 System.out.println("鍙戦佹秷鎭?" + i+"鎴愬姛");
72 producer.send(message);
73 }
74 }
75 }
}