消息服务器ActiveMQ-5.2

最近在使用消息服务器开发项目,为了学习MQ,写几行测试代码是很必要的;

测试很简单,首先要弄明白ActiveMQ的大体流程;如下:

1、启动activemq;(启动后可以登录  http://localhost:8161/admin/   里面可以管理Topic)

2、首先需要生产者,向MQ发送消息

3、消费者订阅消息,并接收订阅的消息

4、根据需要,对接收到的消息进行处理

测试代码需要activemq-all-5.2.0.jar

package com.hisense.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class SendString
{
    public static void main(String[] args) {
        
        ConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        Destination destination = null;
        MessageProducer producer = null;
        String destinationName = null;
        
        try
        {
            connectionFactory = new ActiveMQConnectionFactory("tcp://172.16.128.96:61616");
            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination  = session.createTopic("Hisense");
            producer = session.createProducer(destination);
            TextMessage message = session.createTextMessage();
            
            int i = 0;
            while(true){
                try 
                { 
                    message.setText("我发送的消息,你收到了没有"+i);
                    producer.send(message);
                    Thread.currentThread().sleep(3000);//毫秒 
                    System.out.println(i++);
                } 
                catch(Exception e){} 

            }
        }
        catch(JMSException e)
        {
            e.printStackTrace();
        }finally{
            if(connection != null)
            {
                try
                {
                    connection.close();
                }
                catch(JMSException e)
                {
                    e.printStackTrace();
                }
            }
        }

    } 
}

 

package com.hisense.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ReceiveMQ
{
    

    public static void main(String args[])
    {
        ConnectionFactory connectionFactory = null;
        Connection connection = null;
        MessageConsumer consumer=null;
        Message message = null;
        Session session = null;
        
        try
        {
            connectionFactory = new ActiveMQConnectionFactory("tcp://172.16.128.96:61616");
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination topic = session.createTopic("Hisense");
            consumer = session.createConsumer(topic);
            
            TextMessage txtMessage = (TextMessage)consumer.receive();
            while(true){
                try 
                { 
                    System.out.println("received message:"+txtMessage.getText());
                    Thread.currentThread().sleep(3000);//毫秒 
                } 
                catch(Exception e){}
            }
        }
        catch(JMSException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }  
    
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值