JavaEE 6:如何在独立的 JMS 客户端中注入 JMS 资源?
我无法将 javax.jms.ConnectionFactory
注入到我的独立 JMS 客户端中。 我在下面的代码中的 connectionFactory.createConnection()
处收到 java.lang.NullPointerException
。
JmsClient.java
public class JmsClient {
@Resource(mappedName="jms/QueueConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName="jms/ShippingRequestQueue")
private static Destination destination;
public static void main(String[] args) {
try {
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
ObjectMessage message = session.createObjectMessage();
ShippingRequestQueue shippingRequest = new ShippingRequestQueue(1, "107, Old Street");
message.setObject(shippingRequest);
producer.send(message);
session.close();
connection.close();
System.out.println("Shipping request message sent ..");
} catch (Throwable ex) {
ex.printStackTrace();
}
}
}
我已使用 Glassfish 3.1 管理控制台在 Open MQ (MoM) 创建了相应的连接工厂和目标资源。
有人可以帮助我理解我错过了什么吗?
I can't get javax.jms.ConnectionFactory
injected into my standalone JMS client.
I get a java.lang.NullPointerException
at connectionFactory.createConnection()
in the code below.
JmsClient.java
public class JmsClient {
@Resource(mappedName="jms/QueueConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName="jms/ShippingRequestQueue")
private static Destination destination;
public static void main(String[] args) {
try {
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
ObjectMessage message = session.createObjectMessage();
ShippingRequestQueue shippingRequest = new ShippingRequestQueue(1, "107, Old Street");
message.setObject(shippingRequest);
producer.send(message);
session.close();
connection.close();
System.out.println("Shipping request message sent ..");
} catch (Throwable ex) {
ex.printStackTrace();
}
}
}
I have created the corresponding Connection Factory and Destination Resource at Open MQ (MoM) using Glassfish 3.1 Admin Console.
Could someone help me understand what am I missing?
资源注入仅适用于托管环境,例如 Java EE 应用程序服务器或 Spring 容器。 在独立应用程序中,JNDI 是您唯一的选择。
一般来说,注释是由某些工具/框架处理的,而执行
main()
方法的普通 JVM 根本不包含此类内容。据我所知,JVM 开箱即用处理的唯一注释是编译时@Deprecated
、@Override
和@SuppressWarnings
。回复您的评论:我无权访问这本书,因此我只能猜测它们可能描述了运行应用程序客户端组件,没有< /strong> 独立应用程序客户端。这是不一样的 - 查看 Glassfish EJB 常见问题解答。 ACC 通常部署到应用程序服务器中,并且可以通过 Java Web Start 或不通过 Java Web Start 执行,但以特定于 AS 的方式执行。请参阅 Glassfish 示例(你没有说你的EJB在什么AS中执行)。
Resource injection works only in a managed environment, such as a Java EE application server, or a Spring container, for instance. In a standalone application JNDI is your only choice.
Annotations in general are meant to be processed by some tool/framework, and plain JVM that executes your
main()
method simply does not contain such. The only annotations I know of that are processed by JVM out of the box are compile-time@Deprecated
,@Override
and@SuppressWarnings
.Replying to your comment: I don't have access to the book, so I'll only guess that they probably describe running an application client component and not standalone application client. It's not the same — check Glassfish EJB FAQ. ACCs are normally deployed into an application server and can be executed via Java Web Start or without it, but in an AS-specific way. See Glassfish example (you didn't say what AS your EJB executes in).