我在使用注释资源获取连接工厂时遇到了麻烦。 它适用于JNDI查找,但它没有注释,我的连接工厂为空。
这是我的JNDI查找代码(有效):
ConnectionFactory factory = (ConnectionFactory)context.lookup("/ConnectionFactory");
这是带有注释的代码:
@Resource(mappedName = "java:/ConnectionFactory")
private ConnectionFactory factory;
我尝试使用不同的mappedName:/ ConnectionFactory,java:/ JmsXA,JmsXA等但仍然是nullpointerexception: - /。
如果有人有想法......
Thx!
这是我的班级:
@Stateless
public class ModuleCommunicationHandler implements IModuleCommunicationHandler
{
/** The connection factory. */
@Resource(mappedName = "java:/ConnectionFactory")
private ConnectionFactory factory;
...........
/**
* {@inheritDoc}
*/
@Override
public void sendMessage(JMSMessage jmsMessage, int deliveryMode, int acknowledgeMode) throws TechnicalException
{
try
{
context = new InitialContext();
factory = (ConnectionFactory)context.lookup("/ConnectionFactory");
// Setting the destination - Topic or Queue
destination = (Destination)context.lookup(jmsMessage.getDestination());
connection = factory.createConnection();
session = connection.createSession(false, acknowledgeMode);
sender = session.createProducer(destination);
connection.start();
// Creating the message
message = session.createTextMessage();
message.setText(jmsMessage.getBodyMessage());
// Sending the message
sender.setDeliveryMode(deliveryMode);
sender.send(message);
.....
这是日志:
15:05:32,609 ERROR [STDERR] com.*.*.*.server.exception.ModuleCommunicationException: java.lang.NullPointerException
15:05:32,609 ERROR [STDERR] at com.*.*.*.server.service.ModuleCommunicationHandler.sendMessage(ModuleCommunicationHandler.java:147)
15:05:32,664 ERROR [STDERR] Caused by: java.lang.NullPointerException
15:05:32,664 ERROR [STDERR] at com.*.*.*.server.service.ModuleCommunicationHandler.sendMessage(ModuleCommunicationHandler.java:108)
答案 0 :(得分:1)
没有看到你所有的代码(对于有问题的课程)很难说,但这里有一些常见的嫌疑人:
发生这种情况时,您的日志中是否有任何堆栈跟踪?