1.jms接收的客户端
首先,创建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的名字
String brokerHost = "localhost" ; //OpenMQ server (broker)的ip
String brokerPort = "7676" ; //OpenMQ server (broker)的port
String username = "test"; //test账号必须有可以接受此queue或者topic的权限
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");
// 开始监听
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();
//五秒后关闭监听
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的名字
String brokerHost = "localhost" ; //OpenMQ server (broker)的ip
String brokerPort = "7676" ; //OpenMQ server (broker)的port
String username = "customer"; //customer账号必须有可以发送此queue或者topic的权限
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");
}
}
posted on 2009-04-24 10:42
冰是没有未来的,因为它的永恒 阅读(1774)
评论(0) 编辑 收藏 所属分类:
java