ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, ActiveMQConnection.DEFAULT_BROKER_URL); Thread.currentThread().getThreadGroup().list(); /* java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] */ QueueConnection connection = factory.createQueueConnection(); Thread.currentThread().getThreadGroup().list(); /*2个线程,多了1个 java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] Thread[ActiveMQ Task,5,main] */ //Starts (or restarts) a connection‘s delivery of incoming messages. connection.start(); Thread.currentThread().getThreadGroup().list(); /*6个线程,多了4个 java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] Thread[ActiveMQ Task,5,main] Thread[ActiveMQ Transport: tcp://localhost/127.0.0.1:61616,5,main] Thread[InactivityMonitor ReadCheck,5,main] Thread[InactivityMonitor WriteCheck,5,main] Thread[ActiveMQ Task,5,main] */ /* * createQueueSession(boolean transacted, int acknowledgeMode) * transacted - indicates whether the session is transacted * acknowledgeMode - indicates whether the consumer or the client will acknowledge any messages it receives; * ignored if the session is transacted. * Legal values are Session.AUTO_ACKNOWLEDGE, Session.CLIENT_ACKNOWLEDGE, and Session.DUPS_OK_ACKNOWLEDGE. */ QueueSession session = connection.createQueueSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue("foo.bar"); MessageConsumer consumer = session.createConsumer(queue); Thread.currentThread().getThreadGroup().list(); /*7个线程,多了1个 java.lang.ThreadGroup[name=main,maxpri=10] Thread[main,5,main] Thread[ActiveMQ Task,5,main] Thread[ActiveMQ Transport: tcp://localhost/127.0.0.1:61616,5,main] Thread[InactivityMonitor ReadCheck,5,main] Thread[InactivityMonitor WriteCheck,5,main] Thread[ActiveMQ Task,5,main] Thread[ActiveMQ Session Task,7,main] */ consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message msg) { if (msg instanceof TextMessage) { TextMessage txtMsg = (TextMessage) msg; try { System.out.println(txtMsg.getText()); } catch (JMSException e) { e.printStackTrace(); } } } }); /* TODO:同时运行两次再看状态,127.0.0.1:8161/admin/queues.jsp发消息测试一个队列多个监听效果。 C:\>netstat -na | grep 127.0.0.1:61616 TCP 127.0.0.1:55897 127.0.0.1:61616 ESTABLISHED TCP 127.0.0.1:61616 127.0.0.1:55897 ESTABLISHED */ if (System.in.read() > 0) { session.close(); connection.close(); }
时间: 2024-10-17 15:03:00