log N
log N

Reputation: 945

Error running a simple MDB application

I am trying to run the following program.I am using glassfish server 3.1.2 to enable this MDB to run.Then too I am unanble to run it.

package com.mdb;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.QueueBrowser;
import javax.jms.Message;
import javax.jms.JMSException;
import javax.annotation.Resource;
import java.util.Enumeration;

import javax.ejb.Stateless;
/**
 * The MessageBrowser class inspects a queue and displays the messages it
 * holds.
 */
@Stateless
public class MessageClient {
    @Resource(mappedName = "jms/ConnectionFactory")
    private static ConnectionFactory connectionFactory;
    @Resource(mappedName = "jms/Queue")
    private static Queue queue;

    /**
     * Main method.
     *
     * @param args     the queue used by the example
     */
    public static void main(String[] args) {
        Connection connection = null;

        try {
            System.out.println("1");
            connection = connectionFactory.createConnection();
            System.out.println("2");
            Session session = connection.createSession(
                        false,
                        Session.AUTO_ACKNOWLEDGE);
            QueueBrowser browser = session.createBrowser(queue);
            Enumeration msgs = browser.getEnumeration();

            if (!msgs.hasMoreElements()) {
                System.out.println("No messages in queue");
            } else {
                while (msgs.hasMoreElements()) {
                    Message tempMsg = (Message) msgs.nextElement();
                    System.out.println("Message: " + tempMsg);
                }
            }
        } catch (JMSException e) {
            System.err.println("Exception occurred: " + e.toString());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }
}

The problem is I get the follwing exsception upon runing it.

Exception in thread "main" java.lang.NullPointerException
    at com.mdb.MessageClient.main(MessageClient.java:35)

What may be the problem here?

Upvotes: 1

Views: 90

Answers (1)

Mike Braun
Mike Braun

Reputation: 3769

What you have build is not a MDB. It's a stateless session bean that browses a queue.

A MDB has the @MessageDriven annotation. It's invoked whenever a message comes in.

Apart from that, you might want to use the "lookup" attribute instead of the "mappedName" one. The latter is from an ancient time when people weren't sure yet about anything, and needed a temporary hack to make things magically work.

Your usage of static fields and the static main method inside a stateless bean make no sense at all. If you're accessing your bean via that main method you're not using the bean at all and you're just calling an isolated global-like method. If anything, this might be the source of your NPE.

The fix isn't really simple. You're seemingly completely confused between Java EE and Java SE, and between instances and static methods.

Upvotes: 1

Related Questions