hmm, can you package it to let it be run easily?
*Romain Manni-Bucau* *Twitter: @rmannibucau <https://twitter.com/rmannibucau>* *Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/> *LinkedIn: **http://fr.linkedin.com/in/rmannibucau* *Github: https://github.com/rmannibucau* 2013/4/18 TomeeUser <panve...@gmail.com> > Hi Romain, > > Here is the sample code: > > *Resources configured in tomee.xml* > <Resource id="MyJmsResourceAdapter" type="ActiveMQResourceAdapter"> > BrokerXmlConfig = broker:(tcp://localhost:61616) > ServerUrl = tcp://localhost:61616 > </Resource> > > <Resource id="MyJmsConnectionFactory" > type="javax.jms.ConnectionFactory" > ResourceAdapter = MyJmsResourceAdapter > </Resource> > > <Container id="MyJmsMdbContainer" ctype="MESSAGE"> > ResourceAdapter = MyJmsResourceAdapter > </Container> > > <Resource id="test/ejb/AnswerQueue" type="javax.jms.Queue"> > Destination test/ejb/AnswerQueue > </Resource> > > //////////////////////////////////////////////////////////////////////////// > > > *CachedInfoMessageHandlerBean.java* > package test.ejbs.TomEE.beans; > > import javax.jms.Connection; > import javax.jms.ConnectionFactory; > import javax.jms.DeliveryMode; > import javax.jms.JMSException; > import javax.jms.Message; > import javax.jms.MessageListener; > import javax.jms.MessageProducer; > import javax.jms.Queue; > import javax.jms.Session; > import javax.jms.TextMessage; > > > public class CachedInfoMessageHandlerBean implements MessageListener > { > > private ConnectionFactory connectionFactory; > private Queue answerQueue; > > @Override > public void onMessage(Message message) { > try { > > final TextMessage textMessage = (TextMessage) message; > final String question = textMessage.getText(); > > System.out.println("onMessage called"); > > if ("Hello World!".equals(question)) > { > respond("Hello, Test Case!"); > } else if ("How are you?".equals(question)) > { > respond("I'm doing well."); > } else if ("Still spinning?".equals(question)) > { > respond("Once every day, as usual."); > } > } catch (JMSException e) { > throw new IllegalStateException(e); > } > } > > > private void respond(String text) throws JMSException > { > Connection connection = null; > Session session = null; > > try { > connection = connectionFactory.createConnection(); > connection.start(); > > // Create a Session > session = connection.createSession(false, > Session.AUTO_ACKNOWLEDGE); > > // Create a MessageProducer from the Session to the Topic or > Queue > MessageProducer producer = session.createProducer(answerQueue); > producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); > > // Create a message > TextMessage message = session.createTextMessage(text); > > // Tell the producer to send the message > producer.send(message); > } finally > { > // Clean up > if (session != null) > { > session.close(); > } > if (connection != null) > { > connection.close(); > } > } > } > } > // END > > *ejb-jar.xml* > > <?xml version="1.0" encoding="UTF-8"?> > > <ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee" > version = "3.0" > xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" > xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee > http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"> > <enterprise-beans> > <session> > <ejb-name>CalculatorBean</ejb-name> > > <business-local>test.ejbs.TomEE.headers.CalculatorLocal</business-local> > > <business-remote>test.ejbs.TomEE.headers.CalculatorRemote</business-remote> > <ejb-class>test.ejbs.TomEE.beans.CalculatorBean</ejb-class> > <session-type>Stateless</session-type> > <transaction-type>Container</transaction-type> > </session> > <message-driven> > > <ejb-name>CachedInfoMessageHandlerBean</ejb-name> > > <ejb-class>test.ejbs.TomEE.beans.CachedInfoMessageHandlerBean</ejb-class> > <messaging-type>javax.jms.MessageListener</messaging-type> > <message-destination-type>javax.jms.Queue</message-destination-type> > <activation-config> > <activation-config-property> > > > <activation-config-property-name>destination</activation-config-property-name> > > > <activation-config-property-value>CachedInfoMessageHandlerBean</activation-config-property-value> > </activation-config-property> > <activation-config-property> > > > <activation-config-property-name>destinationType</activation-config-property-name> > > > <activation-config-property-value>javax.jms.Queue</activation-config-property-value> > </activation-config-property> > <activation-config-property> > > > <activation-config-property-name>acknowledgeMode</activation-config-property-name> > > > <activation-config-property-value>Auto-acknowledge</activation-config-property-value> > </activation-config-property> > <activation-config-property> > > > <activation-config-property-name>messageSelector</activation-config-property-name> > > > <activation-config-property-value>state=1</activation-config-property-value> > </activation-config-property> > </activation-config> > > <resource-ref> > > > <res-ref-name>java:comp/env/test.ejbs.TomEE.beans.CachedInfoMessageHandlerBean/connectionFactory</res-ref-name> > <res-type>javax.jms.ConnectionFactory</res-type> > <injection-target> > > > <injection-target-class>test.ejbs.TomEE.beans.CachedInfoMessageHandlerBean</injection-target-class> > <injection-target-name>connectionFactory</injection-target-name> > </injection-target> > </resource-ref> > > <resource-env-ref> > > <resource-env-ref-name>java:test/ejb/AnswerQueue</resource-env-ref-name> > <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type> > <mapped-name>AnswerQueue</mapped-name> > <injection-target> > > > <injection-target-class>test.ejbs.TomEE.beans.CachedInfoMessageHandlerBean</injection-target-class> > <injection-target-name>answerQueue</injection-target-name> > </injection-target> > </resource-env-ref> > > </message-driven> > </enterprise-beans> > </ejb-jar> > > *// TomEEAppClient.java* > package tomeeappclient; > > import java.util.Properties; > import javax.jms.Connection; > import javax.jms.ConnectionFactory; > import javax.jms.JMSException; > import javax.jms.MessageProducer; > import javax.jms.Queue; > import javax.jms.Session; > import javax.jms.TextMessage; > import javax.naming.InitialContext; > > public class TomEEAppClient > { > > // @Resource(mappedName = "AnswerQueue") > private Queue queue; > > // @Resource(mappedName = "MyJmsConnectionFactory") > private ConnectionFactory connectionFactory; > > static > { > System.out.println("Loading System properties"); > System.setProperty("MyJmsConnectionFactory", > > "connectionfactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://localhost:61616"); > // System.setProperty("java:test/ejb/AnswerQueue", > "queue:org.apache.activemq.command.ActiveMQQueue:LISTENER"); > } > public static void main(String[] args) throws Exception > { > TomEEAppClient client = new TomEEAppClient(); > client.process(); > } > private void process() throws Exception > { > Properties p = new Properties(); > p.put("java.naming.factory.initial", > "org.apache.openejb.client.RemoteInitialContextFactory"); > p.put("java.naming.provider.url", > "http://127.0.0.1:8080/tomee/ejb"); > InitialContext ctx = new InitialContext(p); > > String factory = "java:MyJmsConnectionFactory"; > javax.jms.ConnectionFactory cf = (ConnectionFactory) > ctx.lookup(factory); > Connection connection = null; > > try > { > connection = cf.createConnection(); > queue = (Queue)ctx.lookup("java:test/ejb/AnswerQueue"); > connection.start(); > Session session = > connection.createSession(false,Session.AUTO_ACKNOWLEDGE); > MessageProducer messageProducer = session.createProducer(queue); > > TextMessage msg = session.createTextMessage(); > msg.setText("Hello World!"); > msg.setStringProperty("name", "TomEERemoteClient"); > > messageProducer.send(msg); > > } catch (JMSException e) > { > throw new RuntimeException(e); > } finally > { > try > { > if (connection != null) > { > connection.close(); > } > }catch (JMSException e) { }//ignore > } > > > } > > > } > //END > > > > -- > View this message in context: > http://openejb.979440.n4.nabble.com/Can-not-connect-to-Message-Bean-from-remote-client-Null-pointer-tp4662203p4662332.html > Sent from the OpenEJB User mailing list archive at Nabble.com. >