ActiveMQ开发实例3

MessagePublisher类

package activemq.yang;

 
import java.util.Date;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
 
public class MessagePublisher implements Runnable {
    private String url;
    private String user;
    private String password;
    private String topicName;
 
    public MessagePublisher(String topicName, String url, String user,
            String password) {
        this.url = url;
        this.user = user;
        this.password = password;
        this.topicName = topicName;
    }
 
    @Override
    public void run() {
        
        try {
        	ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);  
            Connection connection = connectionFactory.createConnection();
            connection.start();
            System.out.println(Thread.currentThread().getName() + "开启");
            //创建Topic
            //Topic topic = new ActiveMQTopic(this.topicName);
            Session session  = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            Topic topic = session.createTopic(this.topicName);
            MessageProducer sendPublisher = session.createProducer(topic);
            sendPublisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            
            for(int i = 0; i < 5; i++){
            	String text = new Date() + " 现在发送是第" + (i+1) + "条消息";
                sendPublisher.send(session.createTextMessage(text));
                System.out.println("send message :" + text);
//                Thread.sleep(1000);
            }
            
            sendPublisher.close();
            session.close();
            connection.close();
             
//            System.out.println("发布消息线程结束******************************");
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
 
    public String getUrl() {
        return url;
    }
 
    public void setUrl(String url) {
        this.url = url;
    }
 
    public String getUser() {
        return user;
    }
 
    public void setUser(String user) {
        this.user = user;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public String getTopic() {
        return topicName;
    }
 
}

MessageSubscriber类

package activemq.yang;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.Topic;
 
import org.apache.activemq.ActiveMQConnectionFactory;
 
public class MessageSubscriber implements Runnable {
    private String url;
    private String user;
    private String password;
    private String topicName;
 
    public MessageSubscriber(String topicName, String url, String user,
            String password) {
        this.url = url;
        this.user = user;
        this.password = password;
        this.topicName = topicName;
    }
 
    @Override
    public void run() {
        try {
        	
        	ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
            Connection connection = connectionFactory.createConnection();
            Session  session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
             // 创建Topic
            Topic topic = session.createTopic(this.topicName);
            MessageConsumer subscriber = session.createConsumer(topic);
            subscriber.setMessageListener(new TextListener());
            connection.start();
            System.out.println(Thread.currentThread().getName() + "开启");
            
            
            
//            subscriber.close();
//            session.close();
//            connection.close();

      }
        catch (JMSException e) {
            e.printStackTrace();
        } 
        
    }
 
    public String getUrl() {
        return url;
    }
 
    public void setUrl(String url) {
        this.url = url;
    }
 
    public String getUser() {
        return user;
    }
 
    public void setUser(String user) {
        this.user = user;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public String getTopic() {
        return topicName;
    }
}

TextListener类,用来监听

package activemq.yang;

import javax.jms.*;

/**
 * Text消息监听
 * 
 * @author XXXX<br> * 
 */
public class TextListener implements MessageListener {
 
    /**
     * Casts the message to a TextMessage and displays its text.
     * 
     * @param message
     *            the incoming message
     */
    public void onMessage(Message message) {
        TextMessage msg = null;
 
        try {
            if (message instanceof TextMessage) {
                msg = (TextMessage) message;
                System.out.println(Thread.currentThread().getName() +"Recevie message: " + msg.getText());
            } else {
                System.out.println("Message of wrong type: "
                        + message.getClass().getName());
            }
        } catch (JMSException e) {
            System.out.println("JMSException in onMessage(): " + e.toString());
        } catch (Throwable t) {
            System.out.println("Exception in onMessage():" + t.getMessage());
        }
        
    }
}

测试类MyActiveMQDemo
package activemq.yang;

import javax.jms.JMSException;

public class MyActiveMQDemo {
    public static void main(String[] args) throws InterruptedException, JMSException {
        String url = "tcp://localhost:61616";
        String user = null;
        String password = null;
        String topic = "TestTopic";


    
        new Thread(new MessagePublisher(topic,url,user,password), "Publish").start();  
        new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber1").start();
        new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber2").start();
//        new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber3").start();
//        new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber4").start();
//        new Thread(new MessageSubscriber(topic,url,user,password), "Name-Subscriber5").start();



    }
}
执行程序,结果如下:

Publish开启
Name-Subscriber1开启
Name-Subscriber2开启
send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第1条消息
send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第2条消息
send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第3条消息
send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第4条消息
send message :Fri Oct 25 13:11:43 CST 2013 现在发送是第5条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第1条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第1条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第2条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第2条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第3条消息
ActiveMQ Session Task-2 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第3条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第4条消息
ActiveMQ Session Task-2 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第4条消息
ActiveMQ Session Task-1 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第5条消息
ActiveMQ Session Task-2 Recevie message: Fri Oct 25 13:11:43 CST 2013 现在发送是第5条消息
监听显示:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值