Reputation:
I am newbie in JMS and this is my first attempt use it. I use glassfish4. I followed this tutorial I set glassfish resources and in EAR application added message driven bean. Plus I opened 7676 port in firewall. After that I created a SE project (different host,i.e. not the server jvm) with one class SeClient and with the following package seclient;
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnectionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
public class SeClient {
public static void main(String a[]) throws Exception {
// Commands to create Topic
// asadmin --port 4848 create-jms-resource --restype javax.jms.Topic TestTopic
// asadmin --port 4848 create-jms-resource --restype javax.jms.TopicConnectionFactory TestTopicConnectionFactory
String msg = "Hello from remote JMS Client";
SeClient test = new SeClient();
System.out.println("==============================");
System.out.println("Publishig message to Topic");
System.out.println("==============================");
System.out.println();
test.sendMessage2Topic(msg);
System.out.println();
System.out.println("==============================");
System.exit(0);
}
private void sendMessage2Topic(String msg) throws Exception{
// Provide the details of remote JMS Client
Properties props = new Properties();
props.put(Context.PROVIDER_URL, "mq://x.x.x.x:7676");//I use my server's IP
// Create the initial context for remote JMS server
InitialContext cntxt = new InitialContext(props);
System.out.println("Context Created");
// JNDI Lookup for TopicConnectionFactory in remote JMS Provider
TopicConnectionFactory qFactory = (TopicConnectionFactory)cntxt.lookup("TestTopicConnectionFactory");
// Create a Connection from TopicConnectionFactory
Connection connection = qFactory.createConnection();
System.out.println("Connection established with JMS Provide ");
// Initialise the communication session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the message
TextMessage message = session.createTextMessage();
message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
message.setText(msg);
// JNDI Lookup for the Topic in remote JMS Provider
Topic topic = (Topic)cntxt.lookup("TestTopic");
// Create the MessageProducer for this communication
// Session on the Topic we have
MessageProducer mp = session.createProducer(topic);
// Broadcast the message to Topic
mp.send(message);
System.out.println("Message Sent: " + msg);
// Make sure all the resources are released
mp.close();
session.close();
cntxt.close();
}
}
But I get the following error:
Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344) at javax.naming.InitialContext.lookup(InitialContext.java:411) at seclient.SeClient.sendMessage2Topic(SeClient.java:64) at seclient.SeClient.main(SeClient.java:45) Java Result: 1
Please, say what is wrong with my code/actions.
EDIT: I changed password in gf - configuration - server config - JMS hosts - default_JMS_host and added to the code
props.put(Context.SECURITY_PRINCIPAL, "admin");
props.put(Context.SECURITY_CREDENTIALS, "xxxxxx");
But same result.
Upvotes: 0
Views: 1092
Reputation: 26
you need to set the value for the variable CLASSPATH: From your Glassfish lib folder and From your Glassfish modules folder ;c:\Glassfish\lib*;c:\Glassfish\modules*;
Upvotes: 1