MDB Quickstart Example Question
foutjo Feb 22, 2012 7:38 AMCurrently have the following sample message bean deployed on a as-7 server.
Used standalone-full.xml to start the server and the messages on the server console seem to indicate that the message bean and HornetQ started successfully.
Console Log:
06:00:04,868 INFO [org.hornetq.core.server.impl.FileLockNodeManager] (MSC service thread 1-4) Waiting to obtain live lock
06:00:04,869 INFO [org.hornetq.core.server.impl.FileLockNodeManager] (MSC service thread 1-4) Live Server Obtained live lock
06:00:05,129 INFO [org.hornetq.core.remoting.impl.netty.NettyAcceptor] (MSC service thread 1-4) Started Netty Acceptor version 3.2.5.Final-a96d88c lidplin12:5445 for CORE protocol
06:00:05,131 INFO [org.hornetq.core.remoting.impl.netty.NettyAcceptor] (MSC service thread 1-4) Started Netty Acceptor version 3.2.5.Final-a96d88c lidplin12:5455 for CORE protocol
06:00:05,133 INFO [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-4) Server is now live
06:00:05,133 INFO [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-4) HornetQ Server version 2.2.11.Final (HQ_2_2_11_FINAL_AS7, 122) [e7a706f2-5cd8-11e1-9420-e06995db4b43]) started
06:00:05,136 INFO [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-6) trying to deploy queue jms.queue.testQueue
06:00:05,142 INFO [org.jboss.as.messaging] (MSC service thread 1-6) JBAS011601: Bound messaging object to jndi name java:/queue/test
06:00:05,143 INFO [org.jboss.as.messaging] (MSC service thread 1-6) JBAS011601: Bound messaging object to jndi name java:jboss/exported/jms/queue/test
06:00:05,155 INFO [org.jboss.as.messaging] (MSC service thread 1-5) JBAS011601: Bound messaging object to jndi name java:/ConnectionFactory
06:00:05,156 INFO [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-8) trying to deploy queue jms.topic.testTopic
06:00:05,174 INFO [org.jboss.as.messaging] (MSC service thread 1-8) JBAS011601: Bound messaging object to jndi name java:/topic/test
06:00:05,175 INFO [org.jboss.as.messaging] (MSC service thread 1-8) JBAS011601: Bound messaging object to jndi name java:jboss/exported/jms/topic/test
06:00:05,176 INFO [org.jboss.as.messaging] (MSC service thread 1-2) JBAS011601: Bound messaging object to jndi name java:jboss/exported/jms/RemoteConnectionFactory
06:00:05,177 INFO [org.jboss.as.deployment.connector] (MSC service thread 1-3) JBAS010406: Registered connection factory java:/JmsXA
06:00:05,177 INFO [org.jboss.as.messaging] (MSC service thread 1-2) JBAS011601: Bound messaging object to jndi name java:/RemoteConnectionFactory
06:00:05,184 INFO [org.hornetq.ra.HornetQResourceAdapter] (MSC service thread 1-3) HornetQ resource adaptor started
06:00:05,185 INFO [org.jboss.as.connector.services.ResourceAdapterActivatorService$ResourceAdapterActivator] (MSC service thread 1-3) IJ020002: Deployed: file://RaActivatorhornetq-ra
06:00:05,187 INFO [org.jboss.as.deployment.connector] (MSC service thread 1-8) JBAS010401: Bound JCA ConnectionFactory [java:/JmsXA]
06:00:05,203 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss AS 7.1.0.Final "Thunder" started in 2007ms - Started 172 of 249 services (75 services are passive or on-demand)
06:00:05,209 INFO [org.jboss.as.server.deployment] (MSC service thread 1-3) JBAS015876: Starting deployment of "LIDP-ADMI2.jar"
06:00:05,976 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-7) JNDI bindings for session bean named TestBean2 in deployment unit deployment "LIDP-ADMI2.jar" are as follows:
java:global/LIDP-ADMI2/TestBean2!com.lidp.main.TestBeanRemoteInf
java:app/LIDP-ADMI2/TestBean2!com.lidp.main.TestBeanRemoteInf
java:module/TestBean2!com.lidp.main.TestBeanRemoteInf
java:jboss/exported/LIDP-ADMI2/TestBean2!com.lidp.main.TestBeanRemoteInf
java:global/LIDP-ADMI2/TestBean2
java:app/LIDP-ADMI2/TestBean2
java:module/TestBean2
06:00:05,979 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-7) JNDI bindings for session bean named TestBean1 in deployment unit deployment "LIDP-ADMI2.jar" are as follows:
java:global/LIDP-ADMI2/TestBean1!com.lidp.main.TestBeanRemoteInf
java:app/LIDP-ADMI2/TestBean1!com.lidp.main.TestBeanRemoteInf
java:module/TestBean1!com.lidp.main.TestBeanRemoteInf
java:jboss/exported/LIDP-ADMI2/TestBean1!com.lidp.main.TestBeanRemoteInf
java:global/LIDP-ADMI2/TestBean1
java:app/LIDP-ADMI2/TestBean1
java:module/TestBean1
06:00:06,107 INFO [org.jboss.as.ejb3] (MSC service thread 1-4) JBAS014142: Started message driven bean 'MessageBean' with 'hornetq-ra' resource adapter
06:00:06,151 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "LIDP-ADMI2.jar"
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Message Bean:
@MessageDriven(name = "HelloWorldMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/test"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
public class HelloWorldMDB implements MessageListener {
private final static Logger LOGGER = Logger.getLogger(HelloWorldMDB.class
.toString());
public void onMessage(Message rcvMessage) {
TextMessage msg = null;
try {
if (rcvMessage instanceof TextMessage) {
msg = (TextMessage) rcvMessage;
LOGGER.info("Received Message: " + msg.getText());
} else {
LOGGER.warning("Message of wrong type: "
+ rcvMessage.getClass().getName());
}
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
The quickstart example includes a servlet client that shows how to write a message to the queue.
Here is the snippet of code it used by the servlet to connect/start/write to the queue:
@Resource(mappedName = "java:/ConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(mappedName = "java:/queue/test")
private Queue queue;
connection = connectionFactory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(queue);
connection.start();
TextMessage message = session.createTextMessage();
message.setText("This is message ");
messageProducer.send(message);
----------------------------------------------------------------------------------------------------------------------
My question is how can I write to the queue from java program from within the server application?
I tried using the same code from above in a simple java class running on the server but I get errors.
Java Client running on server:
class MessageConn
{
Connection connection = null;
@Resource(mappedName = "java:/ConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(mappedName = "java:/queue/test")
private Queue queue;
public MessageConn()
{
try
{
connection = connectionFactory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(queue);
connection.start();
TextMessage message = session.createTextMessage();
for (int i = 0; i < 5; i++) {
message.setText("This is message " + (i + 1));
messageProducer.send(message);
}
System.out.println("Sending message");
session.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
-------------------------------------------------------------------------------------------------------------------------
Console Log after java client attempts to connect to queue:
06:00:23,496 ERROR [stderr] (EJB default - 1) java.lang.NullPointerException
06:00:23,497 ERROR [stderr] (EJB default - 1) at com.lidp.main.MessageConn.<init>(MessageConn.java:25)
-------------------------------------------------------------------------------------------------------------------------
Should I be using a different method to connect to the queue running from java code from within the server?
Any help is greatly appreciated.
Thanks.