1 package pfs.y2017.m11.mq.activemq.demo01; 2 3 import javax.jms.Connection; 4 import javax.jms.DeliveryMode; 5 import javax.jms.Destination; 6 import javax.jms.ExceptionListener; 7 import javax.jms.JMSException; 8 import javax.jms.Message; 9 import javax.jms.MessageConsumer; 10 import javax.jms.MessageProducer; 11 import javax.jms.Session; 12 import javax.jms.TextMessage; 13 14 import org.apache.activemq.ActiveMQConnection; 15 import org.apache.activemq.ActiveMQConnectionFactory; 16 17 public class App { 18 19 // ActiveMq 的默认用户名 20 private static final String USERNAME = ActiveMQConnection.DEFAULT_USER; 21 // ActiveMq 的默认登录密码 22 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; 23 // ActiveMQ 的链接地址 24 private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL; 25 26 27 public static void main(String[] args) throws Exception { 28 thread(new HelloWorldProducer(), false); 29 thread(new HelloWorldProducer(), false); 30 thread(new HelloWorldConsumer(), false); 31 Thread.sleep(1000); 32 thread(new HelloWorldConsumer(), false); 33 thread(new HelloWorldProducer(), false); 34 thread(new HelloWorldConsumer(), false); 35 thread(new HelloWorldProducer(), false); 36 Thread.sleep(1000); 37 thread(new HelloWorldConsumer(), false); 38 thread(new HelloWorldProducer(), false); 39 thread(new HelloWorldConsumer(), false); 40 thread(new HelloWorldConsumer(), false); 41 thread(new HelloWorldProducer(), false); 42 thread(new HelloWorldProducer(), false); 43 Thread.sleep(1000); 44 thread(new HelloWorldProducer(), false); 45 thread(new HelloWorldConsumer(), false); 46 thread(new HelloWorldConsumer(), false); 47 thread(new HelloWorldProducer(), false); 48 thread(new HelloWorldConsumer(), false); 49 thread(new HelloWorldProducer(), false); 50 thread(new HelloWorldConsumer(), false); 51 thread(new HelloWorldProducer(), false); 52 thread(new HelloWorldConsumer(), false); 53 thread(new HelloWorldConsumer(), false); 54 thread(new HelloWorldProducer(), false); 55 } 56 57 public static void thread(Runnable runnable, boolean daemon) { 58 Thread brokerThread = new Thread(runnable); 59 brokerThread.setDaemon(daemon); 60 brokerThread.start(); 61 } 62 63 public static class HelloWorldProducer implements Runnable { 64 public void run() { 65 try { 66 // Create a ConnectionFactory 67 //ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost"); 68 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEN_URL); 69 70 // Create a Connection 71 Connection connection = connectionFactory.createConnection(); 72 connection.start(); 73 74 // Create a Session 75 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 76 77 // Create the destination (Topic or Queue) 78 Destination destination = session.createQueue("TEST.FOO"); 79 80 // Create a MessageProducer from the Session to the Topic or Queue 81 MessageProducer producer = session.createProducer(destination); 82 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 83 84 // Create a messages 85 String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode(); 86 TextMessage message = session.createTextMessage(text); 87 88 // Tell the producer to send the message 89 System.out.println("Sent message: " + message.hashCode() + " : " + Thread.currentThread().getName()); 90 producer.send(message); 91 92 // Clean up 93 session.close(); 94 connection.close(); 95 } catch (Exception e) { 96 System.out.println("Caught: " + e); 97 e.printStackTrace(); 98 } 99 } 100 } 101 102 public static class HelloWorldConsumer implements Runnable, ExceptionListener { 103 public void run() { 104 try { 105 106 // Create a ConnectionFactory 107 //ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost"); 108 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEN_URL); 109 110 // Create a Connection 111 Connection connection = connectionFactory.createConnection(); 112 connection.start(); 113 114 connection.setExceptionListener(this); 115 116 // Create a Session 117 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 118 119 // Create the destination (Topic or Queue) 120 Destination destination = session.createQueue("TEST.FOO"); 121 122 // Create a MessageConsumer from the Session to the Topic or Queue 123 MessageConsumer consumer = session.createConsumer(destination); 124 125 // Wait for a message 126 Message message = consumer.receive(1000); 127 128 if (message instanceof TextMessage) { 129 TextMessage textMessage = (TextMessage) message; 130 String text = textMessage.getText(); 131 System.out.println("Received: " + text); 132 } else { 133 System.out.println("Received: " + message); 134 } 135 136 consumer.close(); 137 session.close(); 138 connection.close(); 139 } catch (Exception e) { 140 System.out.println("Caught: " + e); 141 e.printStackTrace(); 142 } 143 } 144 145 public synchronized void onException(JMSException ex) { 146 System.out.println("JMS Exception occured. Shutting down client."); 147 } 148 } 149 }