我无法使用注释资源获取我的连接工厂。它适用于 JNDI 查找,但不适用于注释,我的连接工厂为空。
这是我的 JNDI 查找代码(有效):
ConnectionFactory factory = (ConnectionFactory)context.lookup("/ConnectionFactory");
这是我带有注释的代码:
@Resource(mappedName = "java:/ConnectionFactory")
private ConnectionFactory factory;
我尝试使用不同的 mappedName 作为:/ConnectionFactory、java:/JmsXA、JmsXA 等,但仍然是 nullpointerexception :-/。
如果有人有想法...
谢谢 !
这是我的课:
@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)