TIBCO EMS自带JNDI服务
在EMS Admistration Tool中,输入
> show factories
Java代码
Producer.java
public class Producer {
private static String QUEUE_NAME = "study.cloudboy.tibcoEMS.jndi.Queue";
public Producer() {
}
public void sendMessage(String[] messages) throws JMSException, NamingException {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.tibco.tibjms.naming.TibjmsInitialContextFactory");
env.put(Context.PROVIDER_URL, "tibjmsnaming://localhost:7222");
env.put(Context.SECURITY_PRINCIPAL, "userName");
env.put(Context.SECURITY_CREDENTIALS, "password");
InitialContext jndiContext = new InitialContext(env);
ConnectionFactory factory = (ConnectionFactory) jndiContext.lookup("GenericConnectionFactory");
Connection connection = factory.createConnection();
Session session = connection.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(QUEUE_NAME);
MessageProducer msgProducer = session.createProducer(null);
TextMessage msg;
int i;
for (i = 0; i < messages.length; i++) {
msg = session.createTextMessage();
msg.setText(messages[i]);
msgProducer.send(destination, msg);
System.out.println("Published message: " + messages[i]);
}
connection.close();
}
public static void main(String[] args) {
try {
Producer producer = new Producer();
String[] messages = {"Hello", "boy"};
producer.sendMessage(messages);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Consumer.java
public class Consumer implements ExceptionListener {
private static String QUEUE_NAME = "study.cloudboy.tibcoEMS.jndi.Queue";
private int ackMode = Session.AUTO_ACKNOWLEDGE;
void run() throws JMSException, NamingException {
System.out.println("Subscribing to destination: " + QUEUE_NAME + "\n");
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.tibco.tibjms.naming.TibjmsInitialContextFactory");
env.put(Context.PROVIDER_URL, "tibjmsnaming://localhost:7222");
env.put(Context.SECURITY_PRINCIPAL, "userName");
env.put(Context.SECURITY_CREDENTIALS, "password");
InitialContext jndiContext = new InitialContext(env);
ConnectionFactory factory = (ConnectionFactory) jndiContext.lookup("GenericConnectionFactory");
Connection connection = factory.createConnection();
Session session = connection.createSession(false, ackMode);
connection.setExceptionListener(this);
Destination destination = null;
destination = session.createQueue(QUEUE_NAME);
MessageConsumer msgConsumer = session.createConsumer(destination);
connection.start();
Message msg = null;
while (true) {
msg = msgConsumer.receive();
if (msg == null) {
break;
}
if (ackMode == Session.CLIENT_ACKNOWLEDGE
|| ackMode == Tibjms.EXPLICIT_CLIENT_ACKNOWLEDGE
|| ackMode == Tibjms.EXPLICIT_CLIENT_DUPS_OK_ACKNOWLEDGE) {
msg.acknowledge();
}
System.out.println("Received message: " + msg);
}
connection.close();
}
public void onException(JMSException e) {
System.out.println("CONNECTION EXCEPTION: " + e.getMessage());
}
public static void main(String[] args) {
try {
Consumer comsuer = new Consumer();
comsuer.run();
} catch(Exception e) {
e.printStackTrace();
}
}
}