You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.4 KiB

  1. /* Quelle: http://activemq.apache.org/hello-world.html */
  2. package verteiltesysteme.mom;
  3. import org.apache.activemq.ActiveMQConnectionFactory;
  4. import javax.jms.Connection;
  5. import javax.jms.DeliveryMode;
  6. import javax.jms.Destination;
  7. import javax.jms.ExceptionListener;
  8. import javax.jms.JMSException;
  9. import javax.jms.Message;
  10. import javax.jms.MessageConsumer;
  11. import javax.jms.MessageProducer;
  12. import javax.jms.Session;
  13. import javax.jms.TextMessage;
  14. /**
  15. * Hello world!
  16. */
  17. public class ActiveMQHelloWorld {
  18. public static void main(String[] args) throws Exception {
  19. thread(new HelloWorldProducer(), false);
  20. thread(new HelloWorldProducer(), false);
  21. thread(new HelloWorldConsumer(), false);
  22. Thread.sleep(10000);
  23. thread(new HelloWorldConsumer(), false);
  24. thread(new HelloWorldProducer(), false);
  25. thread(new HelloWorldConsumer(), false);
  26. thread(new HelloWorldProducer(), false);
  27. Thread.sleep(10000);
  28. thread(new HelloWorldConsumer(), false);
  29. thread(new HelloWorldProducer(), false);
  30. thread(new HelloWorldConsumer(), false);
  31. thread(new HelloWorldConsumer(), false);
  32. thread(new HelloWorldProducer(), false);
  33. thread(new HelloWorldProducer(), false);
  34. Thread.sleep(10000);
  35. thread(new HelloWorldProducer(), false);
  36. thread(new HelloWorldConsumer(), false);
  37. thread(new HelloWorldConsumer(), false);
  38. thread(new HelloWorldProducer(), false);
  39. thread(new HelloWorldConsumer(), false);
  40. thread(new HelloWorldProducer(), false);
  41. thread(new HelloWorldConsumer(), false);
  42. thread(new HelloWorldProducer(), false);
  43. thread(new HelloWorldConsumer(), false);
  44. thread(new HelloWorldConsumer(), false);
  45. thread(new HelloWorldProducer(), false);
  46. }
  47. public static void thread(Runnable runnable, boolean daemon) {
  48. Thread brokerThread = new Thread(runnable);
  49. brokerThread.setDaemon(daemon);
  50. brokerThread.start();
  51. }
  52. public static class HelloWorldProducer implements Runnable {
  53. public void run() {
  54. try {
  55. // Create a ConnectionFactory
  56. ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
  57. // Create a Connection
  58. Connection connection = connectionFactory.createConnection();
  59. connection.start();
  60. // Create a Session
  61. Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  62. // Create the destination (Topic or Queue)
  63. Destination destination = session.createQueue("TEST.FOO");
  64. // Create a MessageProducer from the Session to the Topic or Queue
  65. MessageProducer producer = session.createProducer(destination);
  66. producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  67. // Create a messages
  68. String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
  69. TextMessage message = session.createTextMessage(text);
  70. // Tell the producer to send the message
  71. System.out.println("Sent message: " + message.hashCode() + " : " + Thread.currentThread().getName());
  72. producer.send(message);
  73. // Clean up
  74. session.close();
  75. connection.close();
  76. } catch (Exception e) {
  77. System.out.println("Caught: " + e);
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82. public static class HelloWorldConsumer implements Runnable, ExceptionListener {
  83. public void run() {
  84. try {
  85. // Create a ConnectionFactory
  86. ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
  87. // Create a Connection
  88. Connection connection = connectionFactory.createConnection();
  89. connection.start();
  90. connection.setExceptionListener(this);
  91. // Create a Session
  92. Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  93. // Create the destination (Topic or Queue)
  94. Destination destination = session.createQueue("TEST.FOO");
  95. // Create a MessageConsumer from the Session to the Topic or Queue
  96. MessageConsumer consumer = session.createConsumer(destination);
  97. // Wait for a message
  98. Message message = consumer.receive(1000);
  99. if (message instanceof TextMessage) {
  100. TextMessage textMessage = (TextMessage) message;
  101. String text = textMessage.getText();
  102. System.out.println("Received: " + text);
  103. } else {
  104. System.out.println("Received: " + message);
  105. }
  106. consumer.close();
  107. session.close();
  108. connection.close();
  109. } catch (Exception e) {
  110. System.out.println("Caught: " + e);
  111. e.printStackTrace();
  112. }
  113. }
  114. public synchronized void onException(JMSException ex) {
  115. System.out.println("JMS Exception occured. Shutting down client.");
  116. }
  117. }
  118. }