1 43 44 import javax.jms.*; 45 import javax.naming.InitialContext ; 46 import javax.naming.Context ; 47 import javax.naming.NamingException ; 48 49 53 public class SimpleReceiver extends Thread { 54 55 public void run() { 56 57 try { 58 Context ctx = new InitialContext (); 60 QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("theQueueConnectionFactory"); 61 Queue queue = (Queue) ctx.lookup("theQueue"); 62 63 QueueConnection connection = factory.createQueueConnection(); 65 QueueSession session = connection.createQueueSession (false, Session.AUTO_ACKNOWLEDGE); 66 QueueReceiver receiver = session.createReceiver(queue); 67 68 connection.start(); 70 TextMessage msg = null; 71 try { 72 while (true) { 73 msg = (TextMessage)receiver.receive(); 74 System.out.println("[SimpleReceiver] received: "+msg.getText()); 75 if (msg.getText().startsWith("LAST ")) break; 76 } 77 } catch (JMSException e) { 78 System.err.println("Exception thrown by receive :"+e.getMessage()); 79 } catch (ClassCastException e) { 80 System.err.println("Received an unknown message of type :"+msg.getClass().getName()); 81 } 82 connection.stop(); 83 84 receiver.close(); 86 session.close(); 87 connection.close(); 88 } catch (Exception e) { 89 System.err.println("Exception thrown from SimpleSender "+e.getMessage()); 90 e.printStackTrace(); 91 } 92 } 93 94 } 95 | Popular Tags |