010_JMS消息选择器

1. 表达式定义的一些重要的规则

1.1. 表达式的操作符不区分大小写, 但通常约定为大写。

1.2. 表达式的变量名跟普通Java变量名的命名规则一致, 区分大小写, 如articleType和articletype是两个不同的变量。

1.3. 表达式的变量名必须是标准的消息头名或者已存在的属性名, 否则对应的值会为NULL。

1.4. 表达式的值不会进行类型转化, 即假设有message.setStringProperty("version", "1"); 那么表达式"version=1"对于message来说, 结果是false。

1.5. 支持基本的算术操作, 逻辑操作, 括号, 取反等运算符。

1.6. 支持BETWEEN .. AND ... , IN(NOT IN)等操作。

1.7. 支持LIKE操作进行模糊匹配, %表示匹配多个任意字符, _表示匹配一个任意字符。

1.8. 支持IS NULL及IS NOT NULL。

1.9. 其它未提到的SQL 92 标准, 可以认为能在WHERE语句后使用的表达式, 在这儿都可以使用。

2. 例子

2.1. 新建一个名为JMSSelector的Java项目, 同时拷入相关jar包

2.2. 编辑MyProducer.java

package com.jmsapp.sel;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class MyProducer {
	// 默认连接用户名
	private static final String dftUsr = ActiveMQConnection.DEFAULT_USER;
	// 默认用户密码
	private static final String dftPwd = ActiveMQConnection.DEFAULT_PASSWORD;
	// 默认连接地址
	private static final String dftUrl = ActiveMQConnection.DEFAULT_BROKER_URL;
	// 队列名称
	private static final String queueName = "queueSelector";

	public static void main(String[] args) {
		// 1. 创建一个连接工厂
		QueueConnectionFactory cf = new ActiveMQConnectionFactory(dftUsr, dftPwd, dftUrl);
		// 连接对象
		QueueConnection conn = null;
		// 会话对象
		QueueSession session = null;
		
		try {
			// 2. 创建连接
			conn = cf.createQueueConnection();
			// 3. 启动连接
			conn.start();
			// 4. 创建会话
			session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			// 5. 创建消息目的地。如果是点对点, 那么它的实现是Queue; 如果是订阅模式, 那它的实现是Topic。这里我们创建一个名为queueSelector的消息队列。
			Queue queue = session.createQueue(queueName);
			// 6. 消息生产者
			QueueSender sender = session.createSender(queue);
			
			// 7. 创建文本消息和发送消息
			TextMessage tm = session.createTextMessage();
			tm.setText("用户支付");
			tm.setStringProperty("component", "userPay");
			sender.send(tm);
			System.out.println("发送: " + tm.getText());
			
			TextMessage msg = session.createTextMessage();
			msg.setText("用户模块");
			msg.setJMSType("userModel");
			sender.send(msg);
			System.out.println("发送: " + msg.getText());
			
			TextMessage message = session.createTextMessage();
			message.setText("商品模块");
			sender.send(message);
			System.out.println("发送: " + message.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			try {
				if (session != null) {
					session.close();
				}
			} catch (JMSException e1) {
				e1.printStackTrace();
			} finally {
				if (conn != null) {
					try {
						conn.close();
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}

2.3. 编辑MyConsumer.java

package com.jmsapp.sel;

import javax.jms.JMSException;
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 org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class MyConsumer {
	// 默认连接用户名
	private static final String dftUsr = ActiveMQConnection.DEFAULT_USER;
	// 默认用户密码
	private static final String dftPwd = ActiveMQConnection.DEFAULT_PASSWORD;
	// 默认连接地址
	private static final String dftUrl = ActiveMQConnection.DEFAULT_BROKER_URL;
	// 队列名称
	private static final String queueName = "queueSelector";

	public static void main(String[] args) {
		// 1. 创建一个连接工厂
		QueueConnectionFactory cf = new ActiveMQConnectionFactory(dftUsr, dftPwd, dftUrl);
		// 连接对象
		QueueConnection conn = null;
		// 会话对象
		QueueSession session = null;
		
		try {
			// 2. 创建连接
			conn = cf.createQueueConnection();
			// 3. 启动连接
			conn.start();
			// 4. 创建会话
			session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			// 5. 创建消息目的地。如果是点对点, 那么它的实现是Queue; 如果是订阅模式, 那它的实现是Topic。这里我们创建一个名为queueSelector的消息队列。
			Queue queue = session.createQueue(queueName);
			// 6. 消息接收者
			QueueReceiver receiver = session.createReceiver(queue, "JMSType ='userModel' or component like 'user%'");
			// 7. 创建文本消息和发送消息
			while(true) {
				TextMessage msg = (TextMessage) receiver.receive(3000);
				if(msg == null) {
					break;
				}
				System.out.println("接收: " + msg.getText());
			}
		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			try {
				if (session != null) {
					session.close();
				}
			} catch (JMSException e1) {
				e1.printStackTrace();
			} finally {
				if (conn != null) {
					try {
						conn.close();
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}

2.4. 运行MyProducer.java

2.5. 运行MyConsumer.java

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值