简体   繁体   中英

How to run a JMS application on Glassfish 4.0 within Eclipse (Juno)

I have been following this tutorial to understand how JMS & Glassfish works. Instead of using Netbeans, I have been using Eclipse (Juno) and have successfully ran the applications (Producer, Synchconsumer, AsynchConsumer, etc.) via command-line: appclient -client nameOFJarFile typeOfMessage numberOfMessages for the producer and appclient -client nameOFJarFile typeOfMessage for the consumers.

I am trying to get this all to work in Eclipse so that I am able to step through the code to see how things work better (as I expect to have to do this for the JMS application that I will be building), but I am unable to get these tutorial files to show up "properly" and run in Eclipse.

By show up properly, I mean: I have imported the simple parent project into Eclipse and have navigated to and opened the .java files. The way I have the IDE set up, each reserved word / variable / everything else is supposed to display in different colors:

rainbowColors

The way it is displaying only shows reserved words and Strings, telling me something is wrong, but I'm not sure what:

synchronousConsumerColors

I have the Glassfish server running in Eclipse, though when I click the Run button and go to Run As, there is no option to run the file on the Glassfish server. Instead, the default option is to run the parent project simple as a Maven build:

RunAs_Nothing

How can I configure these applications to both show up "properly" and to run within the Eclipse IDE's console, just as if I had typed appclient -client synchconsumer.jar queue ?

So you have a couple problems. A big one and a small one.

Big one first. The tutorial example is an appclient. So what you can do is create a stand alone. First create a new Maven project. You will need to attach the javaee-api dependency

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
</dependency>

From there, you will need a few more dependencies to be able to use glassfish and jms for stand alone apps.

<dependency>
  <groupId>org.glassfish.main.appclient</groupId>
  <artifactId>gf-client</artifactId>
  <version>4.0</version>
</dependency>
<dependency>
    <groupId>org.glassfish.mq</groupId>
    <artifactId>imqjmsra</artifactId>
    <version>5.0</version>
</dependency>
<dependency>
    <groupId>org.glassfish.mq</groupId>
    <artifactId>imqbroker</artifactId>
    <version>5.0</version>
</dependency>

These dependencies have a lot of subdepencies, so it may take a while to download. You'll be glad you have them though, for future development. The dependencies are good for EJB look up also. Instead of passing the JNDI name to the lookup() , as you'll see below, just pass the fully qualified class name of the session bean.

So now the Small problem. The program from the tutorial is meant to be ran from the command line. You can make some simple adjustments. Just copy the program (I'll start you off with the Producer class) and make a few changes.

Get rid of the @Resource annotations. Instead you will use javax.naming.InitialContext to lookup the directory. The final code will look like this (assuming you're created the admin objects required to run the program from the command line, as you said you have)

I just hard coded the NUM_MSGS and the desType .

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.jms.JMSRuntimeException;
import javax.jms.Queue;
import javax.jms.Topic;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Producer {

    private static ConnectionFactory connectionFactory;
    private static Queue queue;
    private static Topic topic;

    public static void main(String[] args) throws NamingException {
        final int NUM_MSGS = 10;
        InitialContext ic = new InitialContext();
        connectionFactory = (ConnectionFactory)ic.lookup("java:comp/DefaultJMSConnectionFactory");
        queue = (Queue)ic.lookup("jms/MyQueue");
        topic = (Topic)ic.lookup("jms/MyTopic");

        String destType = "queue";
        System.out.println("Destination type is " + destType);

        if (!(destType.equals("queue") || destType.equals("topic"))) {
            System.err.println("Argument must be \"queue\" or " + "\"topic\"");
            System.exit(1);
        }

        Destination dest = null;

        try {
            if (destType.equals("queue")) {
                dest = (Destination) queue;
            } else {
                dest = (Destination) topic;
            }
        } catch (JMSRuntimeException e) {
            System.err.println("Error setting destination: " + e.toString());
            System.exit(1);
        }

        try (JMSContext context = connectionFactory.createContext();) {
            int count = 0;

            for (int i = 0; i < NUM_MSGS; i++) {
                String message = "This is message " + (i + 1) 
                        + " from producer";
                // Comment out the following line to send many messages
                System.out.println("Sending message: " + message);
                context.createProducer().send(dest, message);
                count += 1;
            }
            System.out.println("Text messages sent: " + count);


            context.createProducer().send(dest, context.createMessage());
            // Uncomment the following line if you are sending many messages
            // to two synchronous consumers
            // context.createProducer().send(dest, context.createMessage());
        } catch (JMSRuntimeException e) {
            System.err.println("Exception occurred: " + e.toString());
            System.exit(1);
        }
        System.exit(0);
    }
}

Credited Links (in helping me solve the same problem you have)

GOOD LUCK!

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