java - 为什么连接 JMS 服务的 java 代码无法编译?

标签 java glassfish jms

我一直在使用 GlassFish 服务器来处理项目,以使用我的 java 代码连接 JMS 服务。 我已经创建了connectionFactory、队列和主题,如下所述。

1. jms/GlassFishBookConnectionFactory connectionfactory
2. jms/GlassFishBookQueue queue
3. jms/GlassFishBookTopic

这是我的 java 代码,用于连接到上述连接的 JMS 服务。我一直按照给定网址的步骤进行处理。 http://www.packtpub.com/article/setting-glassfish-jms-and-working-message-queues 我刚刚开始研究JMS服务。我在这方面的工作并不完美。我已经下载了 java.jms jar 文件并将其解压。

package net.ensode.glassfishbook;

import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.*;

public class MessageSender
{
  @Resource(mappedName = "jms/GlassFishBookConnectionFactory")
  private static ConnectionFactory connectionFactory;
  @Resource(mappedName = "jms/GlassFishBookQueue")
  private static Queue queue;

  public void produceMessages()
  {
    MessageProducer messageProducer;
    TextMessage textMessage;
    try
    {
      Connection connection = connectionFactory.createConnection();
      Session session = connection.createSession(false,
        Session.AUTO_ACKNOWLEDGE);
      messageProducer = session.createProducer(queue);
      textMessage = session.createTextMessage();

      textMessage.setText("Testing, 1, 2, 3. Can you hear me?");
      System.out.println("Sending the following message: "
        + textMessage.getText());
      messageProducer.send(textMessage);

      textMessage.setText("Do you copy?");
      System.out.println("Sending the following message: "
        + textMessage.getText());
      messageProducer.send(textMessage);

      textMessage.setText("Good bye!");
      System.out.println("Sending the following message: "
        + textMessage.getText());
      messageProducer.send(textMessage);

      messageProducer.close();
      session.close();
      connection.close();
    }
    catch (JMSException e)
    {
      e.printStackTrace();
    }
  }
  public static void main(String[] args)
  {
    new MessageSender().produceMessages();
  }
}

当前当我编译此代码时。我收到类似

的错误
Exception in thread "main" java.lang.NullPointerException
    at net.ensode.glassfishbook.MessageSender.produceMessages(MessageSender.java:26)
    at net.ensode.glassfishbook.MessageSender.main(MessageSender.java:58)

最佳答案

显然,资源没有被注入(inject)到您的类中,因为您将其作为独立应用程序运行。

引用您链接的文章:

Before delving into the details of this code, alert readers might have noticed that this class is a standalone Java application as it contains a main method. As this class is standalone, it executes outside the application server. In spite of this, we can see that some resources are injected into it, specifically the connection factory and queue. The reason we can inject resources into this code, even though it runs outside the application server, is because GlassFish includes a utility called appclient.

This utility allows us to "wrap" an executable JAR file and allows it to have access to the application server resources. To execute the previous code, assuming it is packaged in an executable JAR file called jmsptpproducer.jar, we would type the following command in the command line:

appclient -client jmsptpproducer.jar

尝试按上述方式运行应用程序。

关于java - 为什么连接 JMS 服务的 java 代码无法编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21460324/

相关文章:

mysql - 为什么我无法从 JDBC 连接池 ping 我的数据库?

java - GlassFish JDO 和全局对象

java - Tomcat 上的 Spring 3 JMS

java - Kotlin 如何在包含在 Lambda 中时指定返回标签

java - 使用 chronos-report-maven-plugin (Jmeter - Chronos) 显示每个响应结果

eclipse - Glassfish 服务器库在 Eclipse Kepler 中不可用?

java - 当消息因队列大小而被拒绝时,RabbitMQ 不会导致发送失败

javascript - 如何将 jms 消息发送到 activeMQ 并使用 mqttJS 在 javascript 中正确解码它

java - SOLR 每个目标排队的最大请求数超过 HttpDestination + TIMED_WAITING 3000

java - 读取 clob 对象时 Spring jdbc 模板连接关闭异常