棣栧厛,鍒涘緩jms Listener
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.test.jms;
import com.sun.messaging.ConnectionConfiguration;
import com.sun.messaging.ConnectionFactory;
import com.sun.messaging.Queue;
import com.sun.messaging.Topic;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.QueueConnection;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.TopicConnection;
/**
*
* @author ann
*/
public class JMSListener implements MessageListener {
String topicName = "mytopic" ; //瑕佺洃鍚殑topic鍚嶅瓧
String queueName = "myqueue" ; //瑕佺洃鍚殑queue鐨勫悕瀛?/span>
String brokerHost = "localhost" ; //OpenMQ server 錛坆roker錛夌殑ip
String brokerPort = "7676" ; //OpenMQ server 錛坆roker錛夌殑port
String username = "test"; //test璐﹀彿蹇呴』鏈夊彲浠ユ帴鍙楁queue鎴栬卼opic鐨勬潈闄?/span>
String password = "test";
ConnectionFactory connectionFactory = null;
// TopicConnection connection = null;
QueueConnection connection = null;
Destination destination = null;
Session session = null;
MessageConsumer consumer = null;
TextMessage message = null;
public JMSListener(){
}
public void onMessage(Message msg){
if(msg instanceof TextMessage){
TextMessage txt = (TextMessage)msg;
try {
System.out.println("msg : " + txt.getText());
} catch (JMSException ex) {
Logger.getLogger(JMSListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void init() throws Exception{
connectionFactory = new ConnectionFactory();
connectionFactory.setProperty(ConnectionConfiguration.imqBrokerHostName, brokerHost);
connectionFactory.setProperty(ConnectionConfiguration.imqBrokerHostPort, brokerPort);
// connectionFactory.setProperty(ConnectionConfiguration.imqBrokerServiceName,brokerName);
connectionFactory.setProperty(ConnectionConfiguration.imqDefaultUsername,username);
connectionFactory.setProperty(ConnectionConfiguration.imqDefaultPassword,password);
// connection = connectionFactory.createTopicConnection();
connection = connectionFactory.createQueueConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//destination = new Topic(topicName);
destination = new Queue(queueName);
consumer = session.createConsumer(destination);
connection.start();
}
// 娑堣垂娑堟伅
public void consumeMessage() throws JMSException, Exception {
init();
connection.start();
System.out.println("Consumer:->Begin listening");
// 寮濮嬬洃鍚?/span>
consumer.setMessageListener(this);
// Message message = consumer.receive();
}
public void destory() throws JMSException{
consumer.close();
session.close();
connection.close();
System.out.println("Consumer:->stop listening");
}
//鍚姩listen
public static void main(String[] args) throws Exception {
JMSListener listen = new JMSListener();
listen.consumeMessage();
//浜旂鍚庡叧闂洃鍚?/span>
try {
Thread.sleep(5000);
} catch (Exception e) {
}
listen.destory();
}
}
2. 鍒涘緩jms鍙戦佺
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.test.jms;
import com.sun.messaging.ConnectionConfiguration;
import com.sun.messaging.ConnectionFactory;
import com.sun.messaging.Queue;
import com.sun.messaging.Topic;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.TopicConnection;
/**
*
* @author ann
*/
public class Send {
String topicName = "mytopic" ; //瑕佺洃鍚殑topic鍚嶅瓧
String queueName = "myqueue" ; //瑕佺洃鍚殑queue鐨勫悕瀛?/span>
String brokerHost = "localhost" ; //OpenMQ server 錛坆roker錛夌殑ip
String brokerPort = "7676" ; //OpenMQ server 錛坆roker錛夌殑port
String username = "customer"; //customer璐﹀彿蹇呴』鏈夊彲浠ュ彂閫佹queue鎴栬卼opic鐨勬潈闄?/span>
String password = "customer";
ConnectionFactory connectionFactory = null;
//TopicConnection connection = null;
QueueConnection connection = null;
Destination destination = null;
Session session = null;
MessageProducer producer = null;
TextMessage message = null;
public Send(){
try {
init();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void init() throws Exception{
connectionFactory = new ConnectionFactory();
//connectionFactory.setProperty("imqAddressList","localhost:7676");
connectionFactory.setProperty(ConnectionConfiguration.imqBrokerHostName, brokerHost);
connectionFactory.setProperty(ConnectionConfiguration.imqBrokerHostPort, brokerPort);
connectionFactory.setProperty(ConnectionConfiguration.imqDefaultUsername,username);
connectionFactory.setProperty(ConnectionConfiguration.imqDefaultPassword,password);
//connection = connectionFactory.createTopicConnection();
connection = connectionFactory.createQueueConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// destination = new Topic(topicName);
destination = new Queue(queueName);
//session.createTopic(topicName);
producer = session.createProducer(destination);
connection.start();
}
public void send(String msg) throws JMSException{
try {
message = session.createTextMessage();
message.setText(msg);
System.out.println("Producer:->Sending message: " + msg);
producer.send(message);
System.out.println("Producer:->Message sent complete!");
} catch (JMSException ex) {
ex.printStackTrace();
}finally{
connection.close();
}
}
public static void main(String[] args)throws JMSException {
Send send = new Send();
send.send("test user : ann");
}
}

]]>