1 package jms.service; 2 3 import javax.ejb.MessageDrivenBean ; 4 import javax.ejb.MessageDrivenContext ; 5 import javax.ejb.EJBException ; 6 import javax.jms.*; 7 import javax.naming.NamingException ; 8 import javax.naming.InitialContext ; 9 10 18 public class ServiceAvailabilityBean implements MessageDrivenBean , MessageListener { 19 MessageDrivenContext context = null; 20 21 public void setMessageDrivenContext(MessageDrivenContext context) { 22 this.context = context; 23 } 24 public void ejbCreate() throws EJBException { } 25 26 public void ejbRemove() { 27 context = null; 28 } 29 30 public void onMessage(Message msg) { 31 try { 32 TextMessage message = (TextMessage)msg; 33 int zipCode = new Integer (message.getText()).intValue(); 35 if (zipCode<50000) 37 sendMessage(message,"true"); 38 else 39 sendMessage(message,"false"); 40 } catch(Exception e) { 41 System.out.println(e); 43 e.printStackTrace(); 44 } 45 } 46 47 public void sendMessage(TextMessage requestMsg, String serviceAvailable) 48 throws NamingException , JMSException { 49 InitialContext jndiContext = new InitialContext (); 51 QueueConnectionFactory factory = (QueueConnectionFactory) 52 jndiContext.lookup("ConnectionFactory"); 53 QueueConnection connect = factory.createQueueConnection(); 54 QueueSession session = connect.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 55 Queue queue = (Queue)requestMsg.getJMSReplyTo(); 56 QueueSender sender = session.createSender(queue); 57 TextMessage message = session.createTextMessage(); 58 message.setJMSCorrelationID(requestMsg.getJMSMessageID()); 60 message.setText(serviceAvailable); 62 sender.send(message); 63 sender.close(); 64 session.close(); 65 connect.close(); 66 } 67 } 68 69 | Popular Tags |