jms過濾器

handawei_5發表於2010-10-25

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容器。

相關文章