[原创]Weblogic配置JMS,Topic和Queue方式简易实现_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
2
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3143 | 回复: 1   主题: [原创]Weblogic配置JMS,Topic和Queue方式简易实现        下一篇 
xiong.li
注册用户
等级:中士
经验:222
发帖:6
精华:0
注册:1970-1-1
状态:离线
发送短消息息给xiong.li 加好友    发送短消息息给xiong.li 发消息
发表于: IP:您无权察看 2016-12-27 11:08:35 | [全部帖] [楼主帖] 楼主

Weblogic12配置:持久性存储+JMS服务器+JMS模块(子部署)

具体配置请参考:http://bbs.landingbj.com/showtopic.jsp?rootid=263438

图1.png

Myeclipse项目结构

图2.png

              注:weblogic.jar网上有的下,Tool为工具类初始化参数作用

工具类:

package com.landingbj.util;


import java.util.Hashtable;


import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

public class Tool {

public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

public final static String PROVIDER_URL=" t3://localhost:7001";

 @SuppressWarnings({ "rawtypes", "unchecked" })

public InitialContext getInitialContext() throws NamingException{

 Hashtable env=new Hashtable();

 env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);

 env.put(Context.PROVIDER_URL, PROVIDER_URL);

     return new InitialContext(env);

 }

}

JMS以队列(Queue)方式实现

  发送端:

package com.landingbj.queue;



import javax.jms.*;

import javax.naming.InitialContext;

import javax.naming.NamingException;


import com.landingbj.util.Tool;


public class MessageSender {

 public final static String JMS_FACTORY="JMSConnectionFactory";

 public final static String QUEUE="SevenQueue";

  public static void main(String[] args) throws NamingException, JMSException {

  Tool tool=new Tool();

  InitialContext context =tool.getInitialContext();

  QueueSender sender=null;

  QueueSession session=null;

  QueueConnection connection=null;

try {

//获取连接工厂

QueueConnectionFactory factory=(QueueConnectionFactory)context.lookup(JMS_FACTORY);

 connection=factory.createQueueConnection();

connection.start();

 session=connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

//获取队列

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

 sender=session.createSender(queue);

TextMessage  message=session.createTextMessage();

message.setText("love milian");

sender.send(message);

System.out.println("send success");

} catch (Exception e) {

e.printStackTrace();

}finally{

sender.close();

session.close();

connection.close();

}

}

}

接收端:

package com.landingbj.queue;


import javax.jms.*;

import javax.naming.InitialContext;

import com.landingbj.util.Tool;


public class MessageReceive {

public final static String JMS_FACTORY="JMSConnectionFactory";

public final static String QUEUE="SevenQueue";

public static void main(String[] args) {

QueueConnection connection=null;

QueueSession session=null;

try {

 Tool tool=new Tool();

 InitialContext context =tool.getInitialContext();

//获取连接工厂

QueueConnectionFactory factory=(QueueConnectionFactory)context.lookup(JMS_FACTORY);

connection=factory.createQueueConnection();

connection.start();

session=connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

//获取队列

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

QueueReceiver receive=session.createReceiver(queue);

TextMessage message=(TextMessage)receive.receive();

System.out.println(message.getText());

System.out.println("receive success");

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

session.close();

connection.close();

} catch (JMSException e) {

e.printStackTrace();

}

}

}


}

JMS以发布/订阅(Topic)方式实现 

发送端:

package com.landingbj.topic;


import javax.jms.*;

import javax.naming.InitialContext;

import javax.naming.NamingException;


import com.landingbj.util.Tool;


public class MessageSender {

public final static String JMS_FACTORY = "JMSConnectionFactory";

public final static String TOPIC = "SevenTopic";


public static void main(String[] args) throws NamingException {

Tool tool = new Tool();

InitialContext context = tool.getInitialContext();

TopicPublisher publisher = null;

TopicSession session = null;

TopicConnection connection = null;

try {

TopicConnectionFactory factory = (TopicConnectionFactory) context

.lookup(JMS_FACTORY);

connection = (TopicConnection) factory.createTopicConnection();

connection.start();

session = connection.createTopicSession(false,

Session.AUTO_ACKNOWLEDGE);

Topic topic = (Topic) context.lookup(TOPIC);

publisher = session.createPublisher(topic);

TextMessage message = session.createTextMessage();

message.setText("Topic测试连接");

publisher.publish(message);

System.out.println("Topic消息已发送!");

} catch (JMSException e) {

e.printStackTrace();

} finally {

try {

session.close();

connection.close();

} catch (Exception e) {

e.printStackTrace();

}

}


}

}

接收端

package com.landingbj.topic;


import javax.jms.*;

import javax.naming.InitialContext;

import javax.naming.NamingException;


import com.landingbj.util.Tool;


public class MessageReceiver {

public final static String JMS_FACTORY = "JMSConnectionFactory";

public final static String TOPIC = "SevenTopic";


public static void main(String[] args) throws NamingException {

Tool tool = new Tool();

InitialContext context = tool.getInitialContext();

TopicSession session = null;

TopicConnection connection = null;

try {

TopicConnectionFactory factory = (TopicConnectionFactory) context

.lookup(JMS_FACTORY);

connection=(TopicConnection) factory.createTopicConnection();

connection.start();

session = connection.createTopicSession(false,

Session.AUTO_ACKNOWLEDGE);

Topic topic = (Topic) context.lookup(TOPIC);

TopicSubscriber subscriber=session.createSubscriber(topic);

TextMessage message=(TextMessage) subscriber.receive();

System.out.println(message.getText());

subscriber.setMessageListener(new MessageListener() {

public void onMessage(Message msg) {

try {

System.out.println(((TextMessage)msg).getText());

} catch (JMSException e) {

e.printStackTrace();

}

}

});

} catch (JMSException e) {

e.printStackTrace();

}finally{

try {

session.close();

connection.close();

} catch (JMSException e) {

e.printStackTrace();

}

}

}


}

Topic测试步骤:

(1)开启weblogic应用服务器。

(2)运行接受类的代码

(3)运行发送类的代码

(4)Meclipse控制台截图

图3.png

图4.png

Weblogic控制台

图5.png













上帝开片不收费,因为人在做天在看



赞(0)    操作        顶端 
2749699054
注册用户
等级:上士
经验:286
发帖:0
精华:0
注册:2016-12-28
状态:离线
发送短消息息给2749699054 加好友    发送短消息息给2749699054 发消息
发表于: IP:您无权察看 2017-2-17 14:11:48 | [全部帖] [楼主帖] 2  楼

学习学习



赞(0)    操作        顶端 
总帖数
2
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论