Re: [J2EE] 問題,想請問 JMS的Queue的問題

看板java作者 (~!!)時間16年前 (2009/08/09 11:12), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串6/7 (看更多)
各位前輩好 先前的問題,應該是我程式有地方出錯 因此,才會產生這樣的事情 在經簡單的程式測試之後 以下的程式碼是可正常運行的 給有需要的人參考 當然,我對程式還不是很熟,因此如果有錯 請不吝指教 謝謝 在Queue的發送端: import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.jms.*; import javax.naming.*; import javax.xml.soap.*; /** *OK * @author Frederic */ public class SendQueue implements MessageListener { private QueueConnection qConnection = null; private QueueSession qSession = null; private QueueSender qSender = null; private Queue receiveQueue = null; private Queue sendQueue = null; public static void main(String args[]) throws NamingException, JMSException, UnknownHostException { String hostName = args[0]; SendQueue sendQueue = new SendQueue(hostName); sendQueue.sendMessage(); System.exit(1); } public SendQueue(String hostName) throws NamingException, JMSException { QueueConnectionFactory qFactory = null; InitialContext jndi = null; Properties env = new Properties(); //指定要傳送訊息的主機名稱 env.put("org.omg.CORBA.ORBInitialHost", hostName); //指定要傳送訊息的Port env.put("org.omg.CORBA.ORBInitialPort", "3700"); jndi = new InitialContext(env); qFactory = (QueueConnectionFactory) jndi.lookup("jms/QueueConnectionFactory"); qConnection = qFactory.createQueueConnection("guest", "guest"); qSession = qConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); sendQueue = (Queue) jndi.lookup("jms/Queue_RCS"); } public void onMessage(Message message) { System.out.println("這邊不關我的事情!!"); } public void sendMessage() throws UnknownHostException, JMSException { TextMessage myMessage = this.qSession.createTextMessage(); myMessage.setText("Hello!!!"); //利用QueueSender和QueueSession來建立訊息的發送 qSender = qSession.createSender(sendQueue); //送出訊息 qSender.send( myMessage, DeliveryMode.PERSISTENT, Message.DEFAULT_PRIORITY, 1800000); //傳送完之後,就將連線關掉 this.qConnection.close(); } } //================================================================= 在Queue的接收端 import java.io.IOException; import javax.xml.soap.SOAPException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.jms.*; import javax.naming.*; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPMessage; /** * * @author Frederic */ public class ReceiveQueue implements MessageListener { private String broker; private static QueueConnection qConnection = null; private QueueSession qSession = null; private QueueSender qSender = null; private Queue receiveQueue = null; private Queue sendQueue = null; public static void main(String args[]) throws NamingException, JMSException, UnknownHostException { System.out.println("準備要開始監聽!!"); ReceiveQueue receiveQueue = new ReceiveQueue(); } public ReceiveQueue() throws NamingException, JMSException, UnknownHostException { QueueConnectionFactory qFactory = null; InitialContext jndi = null; Properties env = new Properties(); //指定要訂閱訊息的主機名稱 //抓取本機名稱 InetAddress addr = InetAddress.getLocalHost(); String localHostName=addr.getHostName(); env.put("org.omg.CORBA.ORBInitialHost", localHostName); //指定要傳送訊息的Port env.put("org.omg.CORBA.ORBInitialPort", "3700"); jndi = new InitialContext(env); qFactory = (QueueConnectionFactory) jndi.lookup("jms/QueueConnectionFactory"); qConnection = qFactory.createQueueConnection("guest", "guest"); qSession = qConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); receiveQueue = (Queue) jndi.lookup("jms/Queue_RCS"); QueueReceiver qReceiver = qSession.createReceiver(receiveQueue); qReceiver.setMessageListener(this); qConnection.start(); } public void onMessage(Message message) { try { TextMessage myMessage = (TextMessage) message; System.out.println(myMessage.getText()); } catch (JMSException ex) { Logger.getLogger(ReceiveQueue.class.getName()).log(Level.SEVERE, null, ex); } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.172.79.138
文章代碼(AID): #1AVZw3CN (java)
討論串 (同標題文章)
文章代碼(AID): #1AVZw3CN (java)