JavaEE 6:如何在独立的 JMS 客户端中注入 JMS 资源?

发布于 12-03 16:56 字数 1411 浏览 1 评论 0原文

我无法将 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森末i2024-12-10 16:56:26

资源注入仅适用于托管环境,例如 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).

作死小能手2024-12-10 16:56:26

@skip:尝试 @Resource(name="jms/QueueConnectionFactory") 而不是 @Resource(mappedName="jms/QueueConnectionFactory")

name=JNDI 名称,按照 javax.annotation.Resource java 文档。

@skip: try @Resource(name="jms/QueueConnectionFactory") instead of @Resource(mappedName="jms/QueueConnectionFactory")

name=JNDI name as per javax.annotation.Resource java doc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文