ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
Queue(点对点) ~~~ public class TestQueue { public static void QueueProduct() { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://10.10.20.52:61616"); try { Connection conn = connectionFactory.createConnection(); conn.start(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue q = session.createQueue("test-queue"); MessageProducer p = session.createProducer(q); TextMessage msg = session.createTextMessage("hello world"); p.send(msg); p.close(); session.close(); conn.close(); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void QueueConsumer() { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://10.10.20.52:61616"); try { Connection conn = connectionFactory.createConnection(); conn.start(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue q = session.createQueue("test-queue"); MessageConsumer c = session.createConsumer(q); c.setMessageListener(new MessageListener(){ @Override public void onMessage(Message arg0) { TextMessage m = (TextMessage)arg0; try { String tm = m.getText(); System.out.println(tm); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); try { System.in.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } c.close(); session.close(); conn.close(); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ~~~