Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. private static final int SEND_NUM = 5;
  2. private static final String BROKER_URL = "tcp://localhost:61616";
  3. private static final String DESTINATION = "hoo.mq.queue";
  4.  
  5. public static void sendMessage(QueueSession session, javax.jms.QueueSender sender) throws Exception {
  6. for (int i = 0; i < SEND_NUM; i++) {
  7. String message = "msg" + (i + 1) + "#";
  8. MapMessage map = session.createMapMessage();
  9. map.setString("text", message);
  10. map.setLong("time", System.currentTimeMillis());
  11. System.out.println(map);
  12.  
  13. sender.send(map);
  14. //Thread.sleep(1000);
  15. }
  16. }
  17.  
  18. public static void run() throws Exception {
  19.  
  20. QueueConnection connection = null;
  21. QueueSession session = null;
  22. try {
  23. QueueConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
  24. connection = factory.createQueueConnection();
  25. connection.start();
  26. session = connection.createQueueSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
  27. Queue queue = session.createQueue(DESTINATION);
  28. javax.jms.QueueSender sender = session.createSender(queue);
  29. sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  30.  
  31. sendMessage(session, sender);
  32. session.commit();
  33.  
  34.  
  35.  
  36. } catch (Exception e) {
  37. throw e;
  38. } finally {
  39. if (session != null) {
  40. session.close();
  41. }
  42. if (connection != null) {
  43. connection.close();
  44. }
  45. }
  46. }
  47.  
  48. public static void main(String[] args) throws Exception {
  49. QueueSender.run();
  50. }
  51.  
  52. private static final String BROKER_URL = "tcp://localhost:61616";
  53. private static final String TARGET = "hoo.mq.queue?customer.prefetchSize=400";
  54.  
  55. public static void run() throws Exception {
  56.  
  57. QueueConnection connection = null;
  58. QueueSession session = null;
  59. try {
  60. QueueConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,BROKER_URL);
  61. connection = factory.createQueueConnection();
  62. ((ActiveMQConnection)connection).setUseAsyncSend(true);
  63. connection.start();
  64. session = connection.createQueueSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
  65. Queue queue = session.createQueue(TARGET);
  66. javax.jms.QueueReceiver receiver = session.createReceiver(queue);
  67.  
  68. receiver.setMessageListener(new MessageListener() {
  69. public void onMessage(Message msg) {
  70. if (msg != null) {
  71. MapMessage map = (MapMessage) msg;
  72. try {
  73. System.out.println(map.getLong("time") + "接收#" + map.getString("text"));
  74. } catch (JMSException e) {
  75. e.printStackTrace();
  76. }
  77.  
  78. }
  79. }
  80. });
  81.  
  82.  
  83. Thread.sleep(1000*10);
  84.  
  85. session.commit();
  86.  
  87. } catch (Exception e) {
  88. throw e;
  89. } finally {
  90. if (session != null) {
  91. session.close();
  92. }
  93. if (connection != null) {
  94. connection.close();
  95. }
  96. }
  97. }
  98.  
  99. public static void main(String[] args) throws Exception {
  100. QueueReceiver.run();
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement