JMS-接发消息机制

因为要做的项目缘故,最近在学weblogic,同时也接触到了JMS消息机制。好了下面就是我在学习JMS学习的过程中的学习心得。

要使用JMS机制,你要用到weblogic来创建一个JMS服务器,至于weblogic的下载和关于JMS服务器的搭建,我想百度上肯定有很多的例子,在这里我就不用详述咯,如果实在又不了解的可以留言你的邮箱给我,或许我可以帮的了你。

首先,我们先来看一下JMS消息机制的原理部分,JMS是通过weblogic提供的一个服务器(当然,你也可以自己编写一个服务器来替代weblogic所充当的角色),而服务器内部有个消息队列(MessageQueue),用于存放消息,也就是说,发送方每发送一条消息,这条消息都会被存到消息队列内,然后接收方会定期的检查队列,看是否有消息题在里面,如果有,则将它取出来。


其次,我们要了解,无论什么的JMS程序,都逃不出这几个步骤:
1)获得一个weblogic Server 上下文引用。
2)创建连接工厂
3)使用连接创建一个连接
4)使用连接创建一个会话
5)获得一个目的
6)使用会话和目的创建消息的生产者
7)创建消息对象
8)使用连接创建一个需要的发送消息类型的实例
9)使用连接创建的一个队列发送器或主题公布器,然后使用发送器或公布器发送消息



要了,接下来就是程序了。我们先来看一下项目结构。

至于libs文件夹下面的两个jar文件,待会我会上传到我的资源空间,有需要的就去下载吧。
先看一下客户端的代码:
private QueueSender sender=null;
private TextMessage msg=null;
public MsgQueueSender(String[] argv) throws NamingException, JMSException{//构造函数
        //获取weblogic Server上下文引用
Context ctx=StaticFunction.getContext();

//创建连接工厂
QueueConnectionFactory qConFactory=(QueueConnectionFactory) ctx.lookup("weblogic.jms.ConnectionFactory");

//创建一个队列
Queue messageQueue=(Queue) ctx.lookup("jms/MyMDB");

//创建连接
QueueConnection qCon=qConFactory.createQueueConnection();

//创建一个会话
QueueSession session=qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
//创建一个发送者
sender=session.createSender(messageQueue);

//创建一个消息
msg=session.createTextMessage();
}


StaticFunction这个类有一个静态方法getContext(),里面封装着上下文获取的代码
public class StaticFunction {


public static Context getContext(){
Context ctx=null;
String url="t3://localhost:7001";
Properties p=new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, url);
try {
ctx=new InitialContext(p);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ctx;
}
}


好了,接下来看一下客户端的发送消息的函数代码
public void runClient(String str) throws JMSException{
msg.setText("Hello");
sender.send(msg);//利用send方法想目的端发送消息
msg.setText("Welcome to JMS");
sender.send(msg);
msg.setText(str);
sender.send(msg);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
MsgQueueSender mqs=new MsgQueueSender(args);
mqs.runClient("yang");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


好了,客户发送端的代码就全部写完了,接下来我们看一下接收端的代码
接收端的类型有两种:
1)同步接收--类似于socket机制,会造成阻塞,直到有消息为止。
public class SyncMesConsumer {//同步接收者


private QueueReceiver receiver=null;//接收器
private TextMessage msg=null;

public SyncMesConsumer() throws NamingException, JMSException{
Context ctx=StaticFunction.getContext();//获取weblogic上下文

//创建一个连接工厂
QueueConnectionFactory qConFactory=(QueueConnectionFactory) ctx.lookup("weblogic.jms.ConnectionFactory");
Queue messageQueue=(Queue) ctx.lookup("jms/MyMDB");
QueueConnection qCon=qConFactory.createQueueConnection();
QueueSession session=(QueueSession) qCon.createSession(false, Session.AUTO_ACKNOWLEDGE);

//创建消息接收者
receiver=session.createReceiver(messageQueue);

//在调用此方法之前,消息传递被禁止
qCon.start();
}
public void runClient() throws JMSException{
msg=(TextMessage) receiver.receive();
System.out.println("Receiver="+msg.getText());
msg=(TextMessage) receiver.receive();
System.out.println("Receiver="+msg.getText());
msg=(TextMessage) receiver.receive();
System.out.println("Receiver="+msg.getText());
}
public static void main(String[] args) {
try {
SyncMesConsumer consumer=new SyncMesConsumer();
consumer.runClient();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}


2)异步接收--不会造成阻塞
public class AyncMesConsumer implements MessageListener{//异步接受者


/**
* @param args
*/
private int EXPECTED_MESSAGE_COUNT=2;
private int messageCount=0;
private QueueReceiver receiver=null;
public AyncMesConsumer() throws NamingException, JMSException{
//获取weblogic上下文
Context ctx=StaticFunction.getContext();
//创建一个连接工厂
QueueConnectionFactory qConFactory=(QueueConnectionFactory) ctx.lookup("weblogic.jms.ConnectionFactory");
Queue messageQueue=(Queue) ctx.lookup("jms/MyMDB");
QueueConnection qCon=qConFactory.createQueueConnection();
QueueSession session=qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
receiver=session.createReceiver(messageQueue);
receiver.setMessageListener(this);
qCon.start();
}
public boolean expectMoreMessages(){
return messageCount < EXPECTED_MESSAGE_COUNT;
}
public static void main(String[] args) throws NamingException, JMSException {
// TODO Auto-generated method stub
int MAX_TRIES=10;
int tryCount=0;
AyncMesConsumer consumer=new AyncMesConsumer();
while(consumer.expectMoreMessages() && (tryCount<MAX_TRIES)){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tryCount++;
}
}
}
@Override
public void onMessage(Message m) {
// TODO Auto-generated method stub
try {
TextMessage msg=(TextMessage) m;
System.out.println(msg.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
messageCount++;
}


}


至于用哪一种,大家可以根据自己的情况而定。。。。。。。
好了,到目前为止代码就全部编写完毕。
运行weblogic
运行接收端、客户端(这两个的顺序无规定)
就可以看到后台打印出:
Hello
Welcome to JMS
yang




ok~~程序通过。当然这只是JMS点对点接发模式而已,JMS还有发布-订阅模式
不过今晚很晚了,就不写了 改天再写一篇JMS发布-订阅模式的吧。。。
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值