jms过滤器

jms有2种消息发送模式,点对点的,和发布订阅型,如果你期待接收方有回馈,来完成一个

 

操作如:订购一件物品。应该使用点对点,如果是日志记录,可能有多个记录器,则使用发

 

布订阅。

 

消息分为持久性的和非持久性的,订阅者也分持久性的和非持久性的。

(持久的消息在出现故障时 JMS提供者会存储消息,订阅者持久性的是在订阅者不可用时,jms缓存消息,到可用时在发送给订阅者)

 

jms过滤有2中类型,一种基于消息的,一种基于地址的

 

下面代码为基于消息过滤器的简单用法:

 

发送器:

package ch06;

import java.util.Scanner;
import java.util.StringTokenizer;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * MessageFilter Test....
 * 
 * @version 2010-10-25 上午11:44:15
 * @author DaWei han
 * @EMAIL handawei_1@126.com
 */
public class QueueSender {

	public QueueSender() {
		try {
			InitialContext context = new InitialContext();
			factory = (QueueConnectionFactory) context
					.lookup("ConnectionFactory");
			con = factory.createQueueConnection();
			session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			requestQueue = (Queue) context.lookup("jms/RequestQueue");
			con.start();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}

	}

	public void senMessage(String message, Integer propr) {
		try {
			TextMessage msg = session.createTextMessage();
			msg.setText("the message is :" + message + " and the propry is :"
					+ propr);
			msg.setIntProperty("level", propr);
			javax.jms.QueueSender sender = session.createSender(requestQueue);
			sender.send(msg);
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

	public void close() {
		try {
			con.close();
		} catch (JMSException e) {
			e.printStackTrace();
		}
		System.exit(0);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		QueueSender sender = new QueueSender();
		System.out.println("Please enter the message to send eg:50,2");
		Scanner in = new Scanner(System.in);

		while (in.hasNextLine()) {
			String line = in.nextLine();
			if ("".equals(line) || "exit".equals(line)) {
				sender.close();
			}
			StringTokenizer tokenizer = new StringTokenizer(line, ",");
			sender.senMessage(tokenizer.nextToken(), Integer.valueOf(tokenizer
					.nextToken()));
		}

	}

	private Queue requestQueue;
	private QueueConnection con;
	private QueueConnectionFactory factory;
	private QueueSession session;
}

 

 

接收器:

package ch06;

import java.util.Scanner;

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.InitialContext;
import javax.naming.NamingException;

/**
 * MessageFilter Test
 * 
 * @version 2010-10-25 上午11:44:42
 * @author DaWei han
 * @EMAIL handawei_1@126.com
 */
public class QueueReciver01 implements MessageListener {

	public QueueReciver01() {
		try {
			InitialContext context = new InitialContext();
			QueueConnectionFactory factory = (QueueConnectionFactory) context
					.lookup("ConnectionFactory");
			con = factory.createQueueConnection();
			session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			requestQueue=(Queue) context.lookup("jms/RequestQueue");
			con.start();
			String filter="level BETWEEN 1 AND 3";
			receiver = session.createReceiver(requestQueue,filter);
			receiver.setMessageListener(this);
           System.out.println("Reciver01 waiting the message.......");
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		QueueReciver01 reciver = new QueueReciver01();

		System.out.println("press the Enter to Exit.");
		Scanner in = new Scanner(System.in);
		in.nextLine();
		System.exit(1);
	}

	public void onMessage(Message arg0) {
		try {
			TextMessage msg = (TextMessage) arg0;

			System.out.println("the message is :" + msg.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

	private QueueReceiver receiver;
	private QueueConnection con;
	private Queue requestQueue;
	private QueueSession session;
}

 基于jboss容器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值