I created a simple message bean from example and tried to connect it from
remote. but getting error not able to connect. Getting Null pointer. Below
is the code: Please help
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.xerox.TomEE.beans;
import javax.annotation.Resource;
import javax.ejb.MessageDriven;
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;
/**
*
*
*/
@MessageDriven
public class CachedInfoMessageHandlerBean implements MessageListener
{
//@Resource
@Resource(name = "CachedInfoMessageHandlerBeanFactory", type =
ConnectionFactory.class)
private ConnectionFactory connectionFactory;
@Resource(name = "AnswerQueue")
private Queue answerQueue;
@Override
public void onMessage(Message message) {
try {
final TextMessage textMessage = (TextMessage) message;
final String question = textMessage.getText();
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();
}
}
}
}
Client code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tomeeappclient;
import com.xerox.TomEE.headers.*;
import java.util.Properties;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
/**
*
*
*/
public class TomEEAppClient
{
// @Resource
// @Resource(name = "jms/CachedInfoMessageHandlerBeanFactory", type =
ConnectionFactory.class)
@Resource
private ConnectionFactory connectionFactory;
@Resource(name = "CachedInfoMessageHandlerBeanFactory")
private Queue questionQueue;
@Resource(name = "AnswerQueue")
private Queue answerQueue;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception
{
TomEEAppClient client = new TomEEAppClient();
client.process();
}
private void process() throws Exception
{
// TODO code application logic here
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");
//p.put(Context.PROVIDER_URL, "ejbd://127.0.0.1:61616");
InitialContext ctx = new InitialContext(p);
CalculatorRemote calc = (CalculatorRemote)
ctx.lookup("CalculatorBeanRemote");
System.out.println( calc.add(10, 10) );
// System.out.println( calc.divide(4, 2) );
// String factory = "remote:CachedInfoMessageHandlerBeanFactory";
// javax.jms.ConnectionFactory cf = (ConnectionFactory)
ctx.lookup(factory);
// final Connection connection = cf.createConnection();
final Connection connection = connectionFactory.createConnection();
connection.start();
final Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
final MessageProducer questions =
session.createProducer(questionQueue);
final MessageConsumer answers =
session.createConsumer(answerQueue);
sendText("Hello World!", questions, session);
// assertEquals("Hello, Test Case!", receiveText(answers));
String answer = receiveText(answers);
System.out.println("Answer received : " + answer);
}
private void sendText(String text, MessageProducer questions, Session
session) throws JMSException {
questions.send(session.createTextMessage(text));
}
private String receiveText(MessageConsumer answers) throws JMSException
{
return ((TextMessage) answers.receive(1000)).getText();
}
}
Error :
C:\AppServers\tomee\webapps>java -jar
"C:\experiments\TomEEAppClient\dist\TomEEAppClient.jar"
Apr 12, 2013 5:27:59 PM org.apache.openejb.client.EventLogger log
INFO:
RemoteInitialContextCreated{providerUri=http://127.0.0.1:8080/tomee/ejb}
20
Exception in thread "main" java.lang.NullPointerException
at tomeeappclient.TomEEAppClient.process(TomEEAppClient.java:66)
at tomeeappclient.TomEEAppClient.main(TomEEAppClient.java:44)
--
View this message in context:
http://openejb.979440.n4.nabble.com/Can-not-connect-to-Message-Bean-from-remote-client-Null-pointer-tp4662203.html
Sent from the OpenEJB User mailing list archive at Nabble.com.