I have got a running GlassFish-Server (Version 4.1). Now I am interested in creating a simple messaging service using jms and a standalone messaging client. (this should run on a remote machine in a separate JVM.) Unfortunately I still get a NameNotFoundException when I attempt to initialize my QueueConnectionFactory.
This is my code:
import java.util.Properties;
import javax.naming.InitialContext;
import javax.jms.*;
import javax.naming.Context;
public class TestJMSQueue {
public static void main(String[] args) throws Exception{
String msg = "Hello from remote JMS Client";
test.sendMessage2Queue(msg);
System.exit(0);
}
private void sendMessage2Queue(String msg) throws Exception {
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.put(Context.PROVIDER_URL, "mq://localhost:7676");
props.put("connectionFactoryNames" , "TestQueueConnectionFactory");
props.put("queue." + "TestQueue", "TestQueue");
InitialContext ctx = new InitialContext(props);
System.out.println("Context created");
ConnectionFactory qFactory = (ConnectionFactory) ctx.lookup("TestQueueConnectionFactory");
Connection connection = qFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TextMessage message = session.createTextMessage();
message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
message.setText(msg);
Queue queue = (Queue) ctx.lookup("TestQueue");
MessageProducer mp = session.createProducer(queue);
mp.send(message);
System.out.println("Message sent: " + msg);
mp.close();
session.close();
ctx.close();
}
}
The Factory and the Queue exist on the server.
I am following this tutorial and already created the listener for the server. I spent the last hours looking at various resources, trying to resolve the problem, but I didn't find a solution.
Can you please tell me where I went wrong?
Thanks a lot.
Here are a few of the resources I have been looking at:
EDIT:
I also tried it with a jndi.properties file instead of the props.put(...) and the InitialContext(props). And with the adding the following before the InitialContext instantiation:
properties.put("org.omg.CORBA.ORBInitialHost", "<IP>");
properties.put("org.omg.CORBA.ORBInitialPort", "3700");
//as found [here...][2]
//But as I understand it the solution with 3700 is not for JMS so I also tried it with 7676
Didn't work either.
The error:
Exception in thread "main" javax.jms.JMSException: Could not create Transport. Reason: java.io.IOException: Transport scheme NOT recognized: [mq]
at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:33)
at org.apache.activemq.ActiveMQConnectionFactory.createTransport(ActiveMQConnectionFactory.java:232)
at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:245)
at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:214)
at org.apache.activemq.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:161)
at testjmsqueue.TestJMSQueue.sendMessage2Queue(TestJMSQueue.java:60)
at testjmsqueue.TestJMSQueue.main(TestJMSQueue.java:30)
Caused by: java.io.IOException: Transport scheme NOT recognized: [mq]
at org.apache.activemq.util.IOExceptionSupport.create(IOExceptionSupport.java:25)
at org.apache.activemq.transport.TransportFactory.findTransportFactory(TransportFactory.java:171)
at org.apache.activemq.transport.TransportFactory.connect(TransportFactory.java:76)
at org.apache.activemq.ActiveMQConnectionFactory.createTransport(ActiveMQConnectionFactory.java:230)
... 5 more
Caused by: java.io.IOException: Could not find factory class for resource: META-INF/services/org/apache/activemq/transport/mq
at org.apache.activemq.util.FactoryFinder.doFindFactoryProperies(FactoryFinder.java:90)
at org.apache.activemq.util.FactoryFinder.newInstance(FactoryFinder.java:58)
at org.apache.activemq.util.FactoryFinder.newInstance(FactoryFinder.java:47)
at org.apache.activemq.transport.TransportFactory.findTransportFactory(TransportFactory.java:167)
... 7 more
try
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.put(Context.PROVIDER_URL, "mq://localhost:7676");
props.put("connectionFactoryNames" , "TestQueueConnectionFactory");
props.put("queue." + "TestQueue", "TestQueue");
InitialContext ic = new InitialContext(props);
ConnectionFactory qFactory = (ConnectionFactory) ic.lookup("TestQueueConnectionFactory");
Queue queue = (Queue ) ic.lookup("TestQueue");