在glassfish服务器消息队列上发送消息

时间:2012-11-30 07:52:51

标签: glassfish jms

我只是测试在glassfish服务器队列上发送消息,所以我已经在glassfish服务器上创建了这样的JMSResource:

enter image description here

现在我在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)

1 个答案:

答案 0 :(得分:0)

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

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

我的例子:

<强> Receiver.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>