3

我只是测试在 glassfish 服务器队列上发送消息,因此我在 glassfish 服务器上创建了 JMSResource,如下所示:

在此处输入图像描述

现在我在 Netbeans IDE 中创建了一个 java 应用程序。这是我的 Java 文件:-

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;

public class MessageSender
{
  @Resource(mappedName = "jms/ConnectionFactory")
  private static ConnectionFactory connectionFactory;
  @Resource(mappedName = "jms/Queue")
  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);

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

现在,当我运行这个简单的应用程序时,它在创建 ConnectionFactory 时出错:

Exception in thread "main" java.lang.NullPointerException
    at simplesendamq.MessageSender.produceMessages(MessageSender.java:33)
    at simplesendamq.MessageSender.main(MessageSender.java:55)
4

1 回答 1

0

GF 客户端必须使用 JNDI 查找来获取连接工厂和消息目的地(主题/队列)。

因此,您必须形成 JNDI 上下文(在其中设置主机、ORB 端口、其他魔术参数)并在类路径中包含某些库(它需要与 GF 一起使用)。

我的例子:

接收器.java

    import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;

    public class Receiver implements MessageListener {
        public static void main(String[] args) throws JMSException, NamingException {
            try {
                Properties props = new Properties();

                props.setProperty("java.naming.factory.initial",
                                  "com.sun.enterprise.naming.SerialInitContextFactory");
                props.setProperty("java.naming.factory.url.pkgs",
                                  "com.sun.enterprise.naming");
                props.setProperty("java.naming.factory.state",
                                  "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
                props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
                props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

                // Gets the JNDI context
                Context jndiContext = new InitialContext(props);

                // Looks up the administered objects
                ConnectionFactory connectionFactory = (ConnectionFactory)
                        jndiContext.lookup("my.connection.factory");

                Queue queue = (Queue) jndiContext.lookup("my.test.queue");

                // Creates the needed artifacts to connect to the queue
                Connection connection = connectionFactory.createConnection();

                Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

                MessageConsumer consumer = session.createConsumer(queue);

                connection.start();   

            } catch(Exception e) {
                e.printStackTrace();
            }
        }

     @Override
    public void onMessage(Message message) {
        System.out.println("onMsg!");
    }
    }

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>examples.my</groupId>
    <artifactId>jms-test</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>com.sun.messaging</groupId>
            <artifactId>imqjmsra</artifactId>
            <version>4.4</version>
        </dependency>

        <dependency>
            <groupId>javax.jms</groupId>
            <artifactId>jms-api</artifactId>
            <version>1.1-rev-1</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.common</groupId>
            <artifactId>common-util</artifactId>
            <version>3.0</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.appclient</groupId>
            <artifactId>gf-client</artifactId>
            <version>3.0-b74b</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.appclient.server</groupId>
            <artifactId>appclient-server-core</artifactId>
            <version>3.0-b74b</version>
        </dependency>

        <dependency>
            <groupId>com.sun.corba</groupId>
            <artifactId>glassfish-corba-orb</artifactId>
            <version>3.0.1-b001</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.security</groupId>
            <artifactId>security</artifactId>
            <version>3.0-b74b</version>
        </dependency>
    </dependencies>

</project>
于 2012-11-30T15:23:30.713 回答