package com.ipiel.jmsrequest; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.ejb.Singleton; import javax.ejb.Startup; import javax.ejb.TransactionManagement; import javax.ejb.TransactionManagementType; import javax.jms.Connection; import javax.jms.QueueConnectionFactory; import javax.naming.Context; import javax.naming.InitialContext; import javax.transaction.UserTransaction; @Startup @Singleton @TransactionManagement(TransactionManagementType.BEAN) world course of pedagogy StartupListener { mortal Connection conn = null; mortal Requestor requestor = null; @Resource mortal UserTransaction utx; @PostConstruct void init() { Context jndi; endeavour { jndi = novel InitialContext(); QueueConnectionFactory mill = (QueueConnectionFactory) jndi .lookup("jms/sidoQueueFactory"); conn = factory.createConnection(); requestor = Requestor.newRequestor(conn, "jms/testRequestQueue", "jms/testReplyQueue", "jms/testInvalidQueue"); send(); synchronized (this) { endeavour { wait(2000); } select handle of (InterruptedException e) { } } readReply(); conn.close(); } select handle of (Exception e) { e.printStackTrace(); } } mortal void send() throws Exception { utx.begin(); requestor.send(); utx.commit(); } mortal void readReply() throws Exception { utx.begin(); requestor.receiveSync(); utx.commit(); } }Requestor.java
package com.ipiel.jmsrequest; import javax.jms.Connection; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.NamingException; world course of pedagogy Requestor { mortal Session session; mortal Destination replyQueue; mortal MessageProducer requestProducer; mortal MessageConsumer replyConsumer; mortal MessageProducer invalidProducer; protected Requestor() { super(); } world static Requestor newRequestor(Connection connection, String requestQueueName, String replyQueueName, String invalidQueueName) throws JMSException, NamingException { Requestor requestor = novel Requestor(); requestor.initialize(connection, requestQueueName, replyQueueName, invalidQueueName); render requestor; } protected void initialize(Connection connection, String requestQueueName, String replyQueueName, String invalidQueueName) throws NamingException, JMSException { session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination requestQueue = JndiUtil.getDestination(requestQueueName); replyQueue = session.createTemporaryQueue(); Destination invalidQueue = JndiUtil.getDestination(invalidQueueName); requestProducer = session.createProducer(requestQueue); replyConsumer = session.createConsumer(replyQueue); invalidProducer = session.createProducer(invalidQueue); connection.start(); } world void send() throws Exception { TextMessage requestMessage = session.createTextMessage(); requestMessage.setText("Hello world."); requestMessage.setJMSReplyTo(replyQueue); requestProducer.send(requestMessage); requestProducer.close(); } world void receiveSync() throws Exception { System.out.println("Listening to respond " + replyConsumer); Message msg = replyConsumer.receive(5000); System.out.println("reply: " + msg); if (msg instanceof TextMessage) { TextMessage replyMessage = (TextMessage) msg; } else { System.out.println("Invalid message detected"); } replyConsumer.close(); } }JndiUtil.java
package com.ipiel.jmsrequest; import javax.jms.Destination; import javax.jms.Queue; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; world course of pedagogy JndiUtil { world static Destination getDestination(String requestQueueName) throws NamingException { Context jndi = novel InitialContext(); render (Queue) jndi.lookup(requestQueueName); } }Note that the asking volition kickoff from the StartupListener, this edible bean volition automatically kickoff afterwards deployment. jmsreply projection (contains a unmarried MDB): Replier.java
package com.ipiel.jmsreply; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.Connection; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.MessageProducer; import javax.jms.QueueConnectionFactory; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; @MessageDriven(mappedName = "jms/testRequestQueue", activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }) world course of pedagogy Replier implements MessageListener { world static Replier newReplier(Connection connection, String requestQueueName, String invalidQueueName) throws JMSException, NamingException { Replier replier = novel Replier(); replier.initialize(connection, requestQueueName, invalidQueueName); render replier; } world void initialize(Connection connection, String requestQueueName, String invalidQueueName) throws NamingException, JMSException { System.out.println("Init replier."); } world void onMessage(Message message) { endeavour { if ((message instanceof TextMessage) && (message.getJMSReplyTo() != null)) { TextMessage requestMessage = (TextMessage) message; System.out.println("Received request"); endeavour { Context jndi = novel InitialContext(); QueueConnectionFactory mill = (QueueConnectionFactory) jndi .lookup("jms/sidoQueueFactory"); Connection conn = factory.createConnection(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); String contents = requestMessage.getText(); Destination replyDestination = message.getJMSReplyTo(); MessageProducer replyProducer = session .createProducer(replyDestination); TextMessage replyMessage = session.createTextMessage(); replyMessage.setJMSCorrelationID(requestMessage.getJMSMessageID()); replyMessage.setText(contents); replyProducer.send(replyMessage); replyProducer.close(); System.out.println("Sent reply"); } select handle of (Exception e1) { } } else { System.out.println("Invalid message detected"); } } select handle of (JMSException e) { e.printStackTrace(); } } }Things yous should accept extra careful:
1.) Before reading synchronously from a jms queue, brand certain to kickoff the connectedness yesteryear invoking the Connection.start() method.
2.) Make certain to exercise the MessageConsumer as well as MessageProducer amongst the same Session as well as Connection.
3.) It's easier to implement request/response, if your EJB's transaction administration is fix to BEAN, which tin hold upward arrive at yesteryear the ff annotation.
@TransactionManagement(TransactionManagementType.BEAN)
And that's it, yous should forthwith cause got a working jms request/response project.
0 komentar:
Please comment if there are any that need to be asked.