1. JMS消息过滤
JMS消息消费者可以有选择性的接收来自目的地的消息,在创建消费者接口MessageCunsumer的时候就可以指定接收消息的条件。
比如接收优先级<9的消息
package jms.pts;
import java.util.Properties;
import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException;
/** * 同步接收优先级小于9的P2S消息 * * @author liuyan * */ public class JbossFilterSyncMessageReciveriver1 {
public void reciveMessage() {
try { String Connection_Factory = "ConnectionFactory";
Context context = getInitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory) context .lookup(Connection_Factory);
Destination dest = (Destination) context.lookup("jbossJMSTopic");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageConsumer reciver = session.createConsumer(dest, "JMSPriority < 9");
TextMessage textMessage = (TextMessage) reciver.receive();
String messageText = textMessage.getText();
System.out.println("textMessage:" + textMessage);
System.out.println("消息内容" + messageText);
// textMessage.acknowledge();
session.close(); connection.close(); } catch (Exception e) { e.printStackTrace(); }
}
private Context getInitialContext() { …… }
/** * @param args */ public static void main(String[] args) {
JbossFilterSyncMessageReciveriver1 jbossSyncMessageReciveriver = new JbossFilterSyncMessageReciveriver1(); jbossSyncMessageReciveriver.reciveMessage(); }
} |
消息生产者如下
package jms.pts;
import java.util.Properties;
import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException;
/** * P2S消息发送者 * * @author liuyan * */ public class JbossMessageSender {
/** * 发送消息 * * @throws Exception */ public void sendMessage() {
try { String Connection_Factory = "ConnectionFactory";
Context context = getInitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory) context .lookup(Connection_Factory);
Destination dest = (Destination) context.lookup("jbossJMSTopic");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageProducer sender = session.createProducer(dest);
sender.setDeliveryMode(DeliveryMode.PERSISTENT); //sender.setTimeToLive(2000);
sender.setPriority(9);
TextMessage textMessage = session.createTextMessage();
textMessage.setStringProperty("ConType", "txt");
textMessage.setText("hello 速速");
sender.send(textMessage);
System.out.println("停顿5秒钟");
// 等待50秒 Thread.sleep(5000);
System.out.println("停顿5秒钟完毕");
textMessage.setText("welcome to JMS");
sender.send(textMessage); session.close(); connection.close(); } catch (Exception e) { e.printStackTrace(); }
}
private Context getInitialContext() { …… }
/** * @param args * @throws Exception */ public static void main(String[] args) throws Exception {
JbossMessageSender jbossMessageSender = new JbossMessageSender(); jbossMessageSender.sendMessage(); }
} |
此消息发出后,消息消费者并不会从目的接收到该消息,因为不满足消费者的设定条件!消费者只对优先级小于9的消息敏感。
同样的类似于消息类型为txt
MessageConsumer reciver = session.createConsumer(dest, "ConType='txt'"); |