mercredi 28 mai 2014

connecting to JNDI on glassFish server


Vote count:

0




Am trying to code a JMS application, I used the glassFish admin page to build a ConnectionFactory and Qeueu ,I want to know how to let my application know about the jndi built on the server to be able to send messages.



import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.swing.JOptionPane;

public class TestJMS {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enter JMS Client Type");

if (input.equals("1")) {
QueueConnection queueConnection = null;

try {


Hashtable env = new Hashtable();
//env.put(Context.SECURITY_AUTHENTICATION, "none");
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:8080");


Context context = new InitialContext(env);
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context.lookup("jms/ConnectionFactory");

String queueName = "jms/Queue";

Queue queue = (Queue) context.lookup(queueName);

queueConnection = queueConnectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);

QueueSender queueSender = queueSession.createSender(queue);

TextMessage message = queueSession.createTextMessage();
message.setText("This is a TextMessage");
queueSender.send(message);
System.out.println("Message sent.");

} catch (NamingException ex) {
ex.printStackTrace();
System.out.println("Naming Exception");
ex.printStackTrace();
} catch (JMSException ex) {
ex.printStackTrace();
System.out.println("JMS Exception");
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException ex) {
}
}
}
}}}


asked 39 secs ago






Aucun commentaire:

Enregistrer un commentaire