JMS请求/应答示例

首先看一下请求者是如何实现的:
package com.foshanshop.ejb3.app;
import javax.jms.*;
import javax.jms.Queue;

import java.util.*;

import javax.naming.*;
public class Requerstor {

/**
* @param args
*/
private QueueSession session=null;
private QueueConnection conn=null;
private MessageProducer producer=null;
private MessageProducer invalidProducer=null;
private Destination replyQueue=null;
private MessageConsumer replyConsumer;
public void send() throws JMSException
{
TextMessage requestMessage=session.createTextMessage();
requestMessage.setText("Hello world");
requestMessage.setJMSReplyTo(replyQueue);
producer.send(requestMessage);
System.out.println("sent request ");
System.out.println("\t Time: "+System.currentTimeMillis()+"ms");
System.out.println("\t Message ID:"+requestMessage.getJMSMessageID());
System.out.println("\t Correl. ID:"+requestMessage.getJMSCorrelationID());
System.out.println("\t reply to: "+requestMessage.getJMSReplyTo());
System.out.println("\t contents:"+requestMessage.getText());
}
public void receive() throws JMSException
{
Message msg=replyConsumer.receive();
if(msg instanceof TextMessage)
{
TextMessage replyMessage=(TextMessage) msg;
System.out.println("recive reply ");
System.out.println("\t Time: "+System.currentTimeMillis()+"ms");
System.out.println("\t Message ID:"+replyMessage.getJMSMessageID());
System.out.println("\t Correl. ID:"+replyMessage.getJMSCorrelationID());
System.out.println("\t reply to: "+replyMessage.getJMSReplyTo());
System.out.println("\t contents:"+replyMessage.getText());
}
else
{
System.out.println("Invalid message detected");
System.out.println("\t Type: "+msg.getClass().getName());
System.out.println("\t Time: "+System.currentTimeMillis());
System.out.println("\t Message ID:"+msg.getJMSMessageID());
System.out.println("\t Correl. ID:"+msg.getJMSCorrelationID());
System.out.println("\t reply to: "+msg.getJMSReplyTo());

msg.setJMSCorrelationID(msg.getJMSMessageID());
invalidProducer.send(msg);
System.out.println("\t Type: "+msg.getClass().getName());
System.out.println("\t Time: "+System.currentTimeMillis());
System.out.println("\t Message ID:"+msg.getJMSMessageID());
System.out.println("\t Correl. ID:"+msg.getJMSCorrelationID());
System.out.println("\t reply to: "+msg.getJMSReplyTo());
}
}
protected void init()
{
try
{
Properties props=new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
props.setProperty(Context.PROVIDER_URL, "localhost:1099");
props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
InitialContext ctx = new InitialContext(props);

QueueConnectionFactory factory=(QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
conn=factory.createQueueConnection();
session=conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
Destination requestDestination=(Queue)ctx.lookup("queue/requestQueue");
Destination invalidDestination=(Queue)ctx.lookup("queue/invalidQueue");
replyQueue=(Queue)ctx.lookup("queue/replyQueue");
producer=session.createProducer(requestDestination);
invalidProducer=session.createProducer(invalidDestination);
replyConsumer=session.createConsumer(replyQueue);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Requerstor r=new Requerstor();
try
{
r.init();
r.send();
}
catch(Exception e)
{
System.out.println(e.toString()+" Requerstor problems");
}
}

}
下面让我们看一下应答者是如何实现的:
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.*;
@MessageDriven(activationConfig={@ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination",propertyValue="queue/requestQueue"),@ActivationConfigProperty(propertyName="acknowledgeMode",propertyValue="Auto-acknowledge")})
public class Reply implements MessageListener {

private QueueConnection conn;
private QueueSession session;
public void onMessage(Message msg) {
// TODO Auto-generated method stub
try
{
if(msg instanceof TextMessage)
{
TextMessage requestMessage=(TextMessage)msg;
System.out.println("Recived request");
System.out.println("\t Time: "+System.currentTimeMillis()+"ms");
System.out.println("\t Message ID:"+requestMessage.getJMSMessageID());
System.out.println("\t Correl. ID:"+requestMessage.getJMSCorrelationID());
System.out.println("\t reply to: "+requestMessage.getJMSReplyTo());
System.out.println("\t contents:"+requestMessage.getText());

Destination replyDestination=msg.getJMSReplyTo();
Properties props=new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
props.setProperty(Context.PROVIDER_URL, "localhost:1099");
props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
InitialContext ctx = new InitialContext(props);

QueueConnectionFactory factory=(QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
conn=factory.createQueueConnection();
session=conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
MessageProducer producer=session.createProducer(replyDestination);
TextMessage replyMessage=session.createTextMessage();
replyMessage.setText("message have receive");
replyMessage.setJMSCorrelationID(requestMessage.getJMSMessageID());
producer.send(replyMessage);

System.out.println("sent reply");
System.out.println("\t Time: "+System.currentTimeMillis()+"ms");
System.out.println("\t Message ID:"+replyMessage.getJMSMessageID());
System.out.println("\t Correl. ID:"+replyMessage.getJMSCorrelationID());
System.out.println("\t reply to: "+replyMessage.getJMSReplyTo());
System.out.println("\t contents:"+replyMessage.getText());
}
else
{
System.out.println("sent to invalid queue");
}
}
catch(Exception e)
{
System.out.println(e.toString()+"Reply problems");
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值