简体   繁体   中英

Why the java code for connecting to JMS service is not compiling?

I have been using the GlassFish server to work on project to connect with JMS service with my java code. I have created the connectionFactory, queue and Topic as described below.

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

Here is my java code to connect to JMS service for the above connection. I have been following the given url's steps to work on it. http://www.packtpub.com/article/setting-glassfish-jms-and-working-message-queues I am just started to study the JMS service. I am not perfect working on it. I have downloaded the java.jms jar file and extracted it.

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();
  }
}

Currently when i compile this code. i am getting the error like

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)

Apparently the resources are not getting injected to your class because you run it as a standalone application.

Quoting the article you have linked:

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 

Try to run the application as stated above.

Use name instead of mappedName. name is the JNDI name, mappedName is product specific name. See the API doc

@Resource(name="jms/GlassFishBookConnectionFactory")
public static ConnectionFactory connectionFactory;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM