首先配置weblogic,下面是步骤:
1、选择服务——》消息传递——》JMS服务器
2、点击新建存储,选着文件存储,点击下一步
3、填入名称,和目标。目录可以不填会有一个默认目录。点击确定
4、点击新建存储
5、选择目标,并单击完成
选择服务——》消息传递——》JMS模块 选择新建
勾选服务器
勾选“是否要向此JMS系统模块中添加资源”
点击新建
选着连接工厂
填写相关名称等,点击下一步
勾选服务器,并单击完成
再次新建并选择队列如上建立连接工厂
点击新建子部署
选择子部署并选择目标服务器
如上一样新建主题 步骤图解如下:
建立web程序 代码和目录结构如下
代码如下:
package test;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Message;
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.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Send {
/**
* @param args
* @throws NamingException
* @throws JMSException
*/
public void send() throws NamingException, JMSException {
String url = "t3://192.168.93.1:7001";
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
Context context = new InitialContext(properties);
//获得JMS信息连接队列工厂
QueueConnectionFactory connFactory = (QueueConnectionFactory) context.lookup("ConnectionFactory");
//获得JMS信息连接队列
QueueConnection queConn = connFactory.createQueueConnection();
//产生队列session,设置事物模式为false,自动应答消息接收
QueueSession queSession = queConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
//获得队列
javax.jms.Queue queue = (javax.jms.Queue) context.lookup("Queue");
//产生JMS队列发送器
QueueSender queSender = queSession.createSender(queue);
//产生JMS消息
Message msg = queSession.createTextMessage("我的第一个JMS");
System.out.print("--------------"+msg.toString());
//发送数据到JMS
queSender.send(msg);
System.out.println("发送成功");
//关闭操作
queSender.close();
queSession.close();
queConn.close();
}
}
package test;
import java.util.Properties;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
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.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Receiver {
/**
* @param args
* @throws NamingException
* @throws JMSException
*/
public void receive() throws NamingException, JMSException {
String url = "t3://192.168.93.1:7001";
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
Context context = new InitialContext(properties);
//获得JMS信息连接队列工厂
QueueConnectionFactory connFactory = (QueueConnectionFactory) context.lookup("ConnectionFactory");
//获得JMS信息连接队列
QueueConnection queConn = connFactory.createQueueConnection();
//产生队列session,设置事物模式为false,自动应答消息接收
QueueSession queSession = queConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
//获得队列
Queue queue = (Queue) context.lookup("Queue");
QueueReceiver queReceiver = queSession.createReceiver(queue);
queReceiver.setMessageListener(new MessageListener(){
public void onMessage(Message msg) {
String msgText = "";
double d = 0;
try {
if(msg instanceof TextMessage){
msgText = ((TextMessage)msg).getText();
}else if(msg instanceof StreamMessage){
msgText = ((StreamMessage)msg).readString();
d = ((StreamMessage)msg).readDouble();
}else if(msg instanceof BytesMessage){
byte[] bt = new byte[1024];
((BytesMessage)msg).readBytes(bt);
msgText = String.valueOf(bt);
}else if(msg instanceof MapMessage){
msgText = ((MapMessage)msg).getString("name");
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
System.out.println("接收到信息:"+msgText+"----"+d);
}
}
});
queConn.start();
}
}
package test2;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Publisher {
public void publisher() throws NamingException, JMSException{
String url = "t3://localhost:7001";
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
Context context = new InitialContext(properties);
TopicConnectionFactory topFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory");
TopicConnection topConn = topFactory.createTopicConnection();
TopicSession topSession = topConn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) context.lookup("Topic");
TopicPublisher topPublisher = topSession.createPublisher(topic);
Message msg = topSession.createTextMessage("发布杂志");
topPublisher.publish(msg);
topPublisher.close();
topSession.close();
topConn.close();
}
}
package test2;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Requester {
public void requester() throws NamingException, JMSException{
String url = "t3://localhost:7001";
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
Context context = new InitialContext(properties);
TopicConnectionFactory topFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory");
TopicConnection topConn = topFactory.createTopicConnection();
TopicSession topSession = topConn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) context.lookup("Topic");
TopicSubscriber topSubscriber = topSession.createSubscriber(topic);
topConn.start();
topSubscriber.setMessageListener(new MessageListener(){
public void onMessage(Message message) {
// TODO Auto-generated method stub
try {
if(message instanceof TextMessage){
String msg = ((TextMessage)message).getText();
System.out.println("收到消息:"+msg);
}else{
System.out.println(message.getClass().getName());
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
由于是事后截的图 JNDI名字不匹配 请读者注意改过来
结果如下:
可以在控制台中监视queue和Topic也会有相应的结果