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!