在weblogic下面调用JMS的例子。
这个例子的主要功能是,一个Sender和一个Receiver,Sender接收用户输入的数据,Receiver打印出用户输入的数据,例子采用的是队列的方式。
Sender:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Hashtable;
import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext;
public class Sender {
public void send() { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String icf = "weblogic.jndi.WLInitialContextFactory"; String url="t3://192.168.1.214:7001";
// Initialise JNDI properties Hashtable env = new Hashtable(); env.put( Context.INITIAL_CONTEXT_FACTORY, icf ); env.put( Context.PROVIDER_URL, url ); try{ //Lookup final InitialContext initContext = new InitialContext(env); QueueConnectionFactory factory = (QueueConnectionFactory) initContext.lookup("myfactory"); Queue destination = (Queue) initContext.lookup("myqueue"); initContext.close();
//Create JMS Object QueueConnection connection = factory.createQueueConnection(); QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
QueueSender sender = session.createSender(destination);
//Send Message String messageText = null; while(true){ System.out.println("Enter message to send or 'quit':"); messageText = reader.readLine(); if("quit".equals(messageText)) break; TextMessage message = session.createTextMessage(messageText); sender.send(message); }
//Exit System.out.println("Exiting..."); reader.close(); connection.close(); System.out.println("Goodbye!");
}catch(Exception e){ e.printStackTrace(); System.exit(1); }
} public static void main(String[] args) { (new Sender()).send(); } } |
Receiver:
import java.util.Hashtable;
import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueReceiver; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext;
public class Receiver implements MessageListener{
private boolean stop = false; public static void main(String[] args) { (new Receiver()).receive(); }
public void receive() { String icf = "weblogic.jndi.WLInitialContextFactory"; String url="t3://192.168.1.214:7001";
// Initialise JNDI properties Hashtable env = new Hashtable(); env.put( Context.INITIAL_CONTEXT_FACTORY, icf ); env.put( Context.PROVIDER_URL, url );
try{ //Look up administratered objects final InitialContext initContext = new InitialContext(env); QueueConnectionFactory factory = (QueueConnectionFactory) initContext.lookup("myfactory"); Queue destination = (Queue) initContext.lookup("myqueue"); initContext.close();
//Create JMS objects QueueConnection connection = factory.createQueueConnection(); QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
QueueReceiver receiver= session.createReceiver(destination); receiver.setMessageListener(this); connection.start(); System.out.println("服务启动"); //Wait for stop while(!stop){ Thread.sleep(1000); }
//Exit System.out.println("Exiting..."); connection.close(); System.out.println("Goodbye!");
}catch(Exception e){ e.printStackTrace(); System.exit(1); } }
public void onMessage(Message message) { try { String msgText = ((TextMessage)message).getText(); System.out.println(msgText); if("stop".equals(msgText)){ stop = true; } } catch (JMSException e) { e.printStackTrace(); stop = true; }
}
} |
设置JMS连接工厂:
设置JMS队列:选择一个JMS服务,点击目标,选择新建queue或topic
检查JNDI,myfactory和myqueue都存在了。
1. String icf = "weblogic.jndi.WLInitialContextFactory";在weblogic中,需要使用此初始化上下文工厂类,这个参数需要根据不同的提供商进行修改,比如在websphere下面,就要改成如下的设置:
String icf = "com.ibm.websphere.naming.WsnInitialContextFactory";
String url="iiop://localhost:2809";
2. 网络资源
JMS的配置的网络资源,可以参照bea的文档,这里就不再废话了。http://edocs.bea.com/wls/docs81/ConsoleHelp/jms_config.html#1105204
3. 通过用户名和密码的连接
env.put ("java.naming.security.authentication", authentication);
env.put ("java.naming.security.principal", userID);
env.put ("java.naming.security.credentials", userPWD);
1. 启动weblogic
2. 启动Receiver
D:/eclipse3.1/workspace/test/bin>java com.wonder.jms.receive.Receiver
服务启动
3. 启动Sender
D:/eclipse3.1/workspace/test/bin>java com.wonder.jms.sender.Sender
Enter message to send or 'quit':
4.在Receiver中打印出了输入的字符。
Write by wonder
2006-7-5