Why does JNDI lookup work in my MDB, but @Resource fail?
gberish Nov 21, 2015 8:54 PMThis link offers an elegant alternative to using the full JMS API to deploy a Message Driven Bean able to receive messages and reply.
https://blogs.oracle.com/arungupta/entry/simple_jms_2_0_sample
But like so many things JBoss, it doesn't work as shown. Can anyone tell me why?
Here's what I pasted below:
1. The MDB code,
2. The xml I added to standalone-full.xml to create three identical Queues
a. svrReceiveQueue (listened to by the MDB)
b. svrSendQueue (used by the MDB to send a reply)
c. svrTestQueue (a dummy queue to see if I could inject it)
3. An extract from the WildFly server log with the output from my MDB's println() statements.
The MDB works fine. A separate standalone java client sends it a message. The MDB receives it. It sends a reply. And the separate java client receives it.
So obviously the @MessageDriven annotations work and the MDB listens to svrReceiveQueue.
And the JNDI lookups work because it sends a reply to svrSendQueue.
But none of the @Resource annotations I used to inject svrTestQueue worked.
Those I commented out fail with a notification of "missing/unsatisfied dependencies" (but no Exception that hints why.)
The one @Resource line that I did not comment out just returns null (see sever log).
Note: In the MDB annotations I had to leave IN "java:" in the JNDI name to make it work.
In the MDB constructor() I had to leave OUT "java:" from the JNDI name to make it work.
Don't know why. Just trial and error.
MDB CODE:
package org.america3.gotest.server.messaging;
ALL IMPORTS
@MessageDriven(
activationConfig ={
@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="maxSession",propertyValue="1"),
@ActivationConfigProperty(propertyName="destination", propertyValue="java:/jms/goSvrReceiveQueue")},
mappedName = "JmsTestMsgBean")
public class JmsTestMsgBean implements MessageListener {
/*
* these tries all failed with "missing/unsatisfied dependencies.
*
* @Resource(mappedName = "jboss/exported/jms/goSvrTestQueue")
* private Queue svrTestQueue;
*
* @Resource(name = "jboss/exported/jms/goSvrTestQueue")
* private Queue svrTestQueue;
*
* @Resource(mappedName = "jms/goSvrTestQueue")
* private Queue svrTestQueue;
*
* @Resource(mappedName = "jms/goSvrTestQueue")
* private Queue svrTestQueue;
*/
/*
* And this try just returned null see output below
*/
@Resource(name = "jms/goSvrTestQueue")
private Queue svrTestQueue;
private Connection connection = null;
private Session session = null;
private Queue svrSendQueue = null;
private Destination sendToDestination = null;
private MessageProducer msgProducer = null;
private final String jmsSelectorKey = "MyMessages";
// constructor
public JmsTestMsgBean () {
try {
System.out.print("\nMDB:constructor(): beg");
Context ctx = new InitialContext();
System.out.print("\n MDB:constructor(): obtained InitialContext");
System.out.print("\n : "+ ctx);
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("java:/jboss/DefaultJMSConnectionFactory");
System.out.print("\n MDB:constructor(): Connection Factory looked up");
System.out.print("\n : " + factory);
this.connection = factory.createConnection("jmsuser", "jmsuser@123");
System.out.print("\n MDB:constructor(): Connection established");
System.out.print("\n : " + this.connection);
this.svrSendQueue = (Queue) ctx.lookup("jboss/exported/jms/goSvrSendQueue");
System.out.print("\n MDB:constructor(): svrSendQueue looked up");
System.out.print("\n : " + this.svrSendQueue);
System.out.print("\n MDB:constructor(): svrTestQueue injected");
System.out.print("\n : " + this.svrTestQueue);
this.sendToDestination = (Destination) this.svrSendQueue;
System.out.print("\n MDB:constructor(): svrSendQueue cast to Destination");
this.session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.print("\n MDB:constructor(): Session instantiated");
System.out.print("\n : " + this.session);
this.msgProducer = session.createProducer(sendToDestination);
System.out.print("\n MDB:constructor(): Producer instantiated");
System.out.print("\n : " + this.msgProducer);
System.out.print("\nMDB:constructor(): end");
}catch(Exception e) {
System.out.print("\n MDB:constructor(): Exception - " + e.getClass().getName());
System.out.print("\n : msg - " + e.getMessage());
System.out.print("\n MDB:constructor(): closeing any connection");
if(this.connection != null)
try {
this.connection.close();
} catch (JMSException e1) {
e1.printStackTrace();
}
System.out.print("\nMDB:constructor(): end - w/Exception");
}
}
@PostConstruct
public void myInit () {
System.out.print("\nMDB:myInit() : Construction completed. ver #10");
}
@PreDestroy
public void myDestroy () throws JMSException {
System.out.print("\nMDB:myDestroy() : called to close any connection");
}
public void onMessage(Message msg) {
System.out.print("\nMDB:onMessage(): begin");
try {
String textReceived = (String)(((ObjectMessage)msg).getObject());
System.out.print("\n MDB:onMessage() : received message = \"" + textReceived + "\"");
String msgSelectorValue = msg.getStringProperty(this.jmsSelectorKey);
System.out.print("\n MDB:onMessage() : selector:");
System.out.print("\n : \"" + msgSelectorValue +"\"");
ObjectMessage replyMsg = session.createObjectMessage();
System.out.print("\n MDB:onMessage() : reply message created");
replyMsg.setObject(new String("Hi. Received your message ok."));
System.out.print("\n MDB:onMessage() : added String as reply Object:");
System.out.print("\n : \"Hi. Received your message ok.\"");
replyMsg.setStringProperty(this.jmsSelectorKey, msgSelectorValue);
System.out.print("\n MDB:onMessage() : setting selector property");
System.out.print("\n : Key");
System.out.print("\n : \"" + this.jmsSelectorKey + "\"");
System.out.print("\n : Value");
System.out.print("\n : \"" + msgSelectorValue + "\"");
msgProducer.send(replyMsg);
System.out.print("\n MDB:onMessage() : message sent");
System.out.print("\nMDB:onMessage() : end");
} catch (JMSException e) {
System.out.print("\n MDB:onMessage() : Exception - " + e.getClass().getName());
System.out.print("\n MDB:onMessage() : msg - " + e.getMessage());
System.out.print("\n MDB:onMessage() : close any connection");
if(this.connection != null) {
try {
this.connection.close();
} catch (JMSException e1) {
e1.printStackTrace();
}
}
System.out.print("\nMDB:onMessage(): end - w/Exception");
}
}
}
2. ADDITIONS TO standalone.xml
<jms-queue name="goSvrReceiveQueue">
<entry name="java:/jboss/exported/jms/goSvrReceiveQueue"/>
<durable>true</durable>
</jms-queue>
<jms-queue name="goSvrSendQueue">
<entry name="java:/jboss/exported/jms/goSvrSendQueue"/>
<durable>true</durable>
</jms-queue>
<jms-queue name="goSvrTestQueue">
<entry name="java:/jboss/exported/jms/goSvrTestQueue"/>
<durable>true</durable>
</jms-queue>
- 3. EXTRACT FROM server.log
### Started Server. MDB NOT deployed
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256M; support was removed in 8.0
14:33:32,895 INFO JBoss Modules version 1.4.3.Final
14:33:33,112 INFO JBoss MSC version 1.2.6.Final
14:33:33,186 INFO WFLYSRV0049: WildFly Full 9.0.2.Final (WildFly Core 1.0.2.Final) starting
...
...
14:33:36,324 INFO WFLYSRV0025: WildFly Full 9.0.2.Final (WildFly Core 1.0.2.Final) started in 3827ms - Starte
d 248 of 422 services (230 services are lazy, passive or on-demand)
### Server started
### Dropped MDB ear pagkage into WildFly/standalone/deployments
14:35:10,219 INFO WFLYDR0001: Content added at location ...\standalone
14:35:10,246 INFO WFLYSRV0027: Starting deployment of "jmsTest.ear" (runtime-name: "jmsTest.ear")
14:35:10,316 INFO WFLYSRV0207: Starting subdeployment (runtime-name: "jmsTest.jar")
14:35:10,611 INFO WFLYEJB0042: Started message driven bean 'JmsTestMsgBean' with 'hornetq-ra.rar' resource adapter
14:35:10,717 INFO WFLYSRV0010: Deployed "jmsTest.ear" (runtime-name : "jmsTest.ear")
### Server instantiates two MDBs
14:35:10,679 INFO
14:35:10,682 INFO MDB:constructor(): beg
14:35:10,683 INFO MDB:constructor(): obtained InitialContext
14:35:10,684 INFO : javax.naming.InitialContext@322fdbef
14:35:10,684 INFO MDB:constructor(): Connection Factory looked up
14:35:10,753 INFO : org.hornetq.ra.HornetQRAConnectionFactoryImpl@3cb794fc
14:35:10,753 INFO MDB:constructor(): Connection established
14:35:10,754 INFO : org.hornetq.ra.HornetQRASessionFactoryImpl@5bbb3863
14:35:10,755 INFO MDB:constructor(): svrSendQueue looked up
14:35:10,755 INFO : HornetQQueue[goSvrSendQueue]
14:35:10,755 INFO MDB:constructor(): svrTestQueue injected
14:35:10,755 INFO : null
14:35:10,756 INFO MDB:constructor(): svrSendQueue cast to Destination
14:35:10,756 INFO MDB:constructor(): Session instantiated
14:35:10,767 INFO : org.hornetq.ra.HornetQRASession@6868ed03
14:35:10,767 INFO MDB:constructor(): Producer instantiated
14:35:10,768 INFO : org.hornetq.ra.HornetQRAMessageProducer@56d67485
14:35:10,779 INFO MDB:constructor(): end
14:35:10,780 INFO MDB:constructor(): beg
14:35:10,780 INFO MDB:constructor(): obtained InitialContext
14:35:10,781 INFO : javax.naming.InitialContext@1d15746d
14:35:10,781 INFO MDB:constructor(): Connection Factory looked up
14:35:10,785 INFO : org.hornetq.ra.HornetQRAConnectionFactoryImpl@3cb794fc
14:35:10,786 INFO MDB:constructor(): Connection established
14:35:10,786 INFO : org.hornetq.ra.HornetQRASessionFactoryImpl@7b913ed5
14:35:10,786 INFO MDB:constructor(): svrSendQueue looked up
14:35:10,787 INFO : HornetQQueue[goSvrSendQueue]
14:35:10,787 INFO MDB:constructor(): svrTestQueue injected
14:35:10,787 INFO : null
14:35:10,787 INFO MDB:constructor(): svrSendQueue cast to Destination
14:35:10,788 INFO MDB:constructor(): Session instantiated
14:35:10,788 INFO : org.hornetq.ra.HornetQRASession@71b4fc0f
14:35:10,789 INFO MDB:constructor(): Producer instantiated
14:35:10,789 INFO : org.hornetq.ra.HornetQRAMessageProducer@3a03782