win7下安装RabbitMQ消息服务器 + 读写队列

 RabbitMQ是什么 ?html

 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。java

1:安装RabbitMQ须要先安装Erlang语言开发包。下载地址 http://www.erlang.org/download.html 在win7下安装Erlang最好默认安装。windows

      配置环境变量 ERLANG_HOME C:\Program Files (x86)\erl5.9 浏览器

      添加到PATH  %ERLANG_HOME%\bin;this

2:安装RabbitMQ 下载地址 http://www.rabbitmq.com/download.html  安装教程:http://www.rabbitmq.com/install-windows.htmlspa

      配置环境变量 C:\Program Files (x86)\RabbitMQ Server\rabbitmq_server-2.8.0orm

      添加到PATH %RABBITMQ_SERVER%\sbin;server

3:进入%RABBITMQ_SERVER%\sbin 目录以管理员身份运行 rabbitmq-plugins.bathtm

     安装完成以后以管理员身份启动 rabbitmq-service.bat教程

4:浏览器访问localhost:55672  默认帐号:guest  密码:guest

建立队列名称为queue_sina ,java示例代码读写队列中queue_sina的消息queue_sina

private static final String exchangeName = "sina";

private static final String exchangeRoutingKey = "sina";

HashMap<String,String> map = new HashMap<String,String>();

map.put("text", request.getText());

map.put("image", imageUrl);

map.put("nick_name", this.getUserName(request.getUserid()));

map.put("shop_name", request.getShopname());

String tousu_map = gson.toJson(map, new TypeToken<HashMap<String,String>>(){}.getType());

System.out.println("tousu_map" + tousu_map);

//写入队列

Producer.sendMsg(PropsUtils.getInstance().getProperty(Constants.EXCHANGE_NAME,

exchangeName), PropsUtils.getInstance()

.getProperty(Constants.EXCHANGE_ROUTING_KEY,

exchangeRoutingKey), tousu_map);

 

//写入队列模版类

public class Producer {

private static AmqpTemplate amqpTemplate = null;

static {

ApplicationContext context = new AnnotationConfigApplicationContext(TousuConfiguration.class);

amqpTemplate = context.getBean(AmqpTemplate.class);

}

public static void sendMsg(String exchangeName,String routingKey,Object message){

amqpTemplate.convertAndSend(exchangeName, routingKey,message);

System.out.println("exchangeName: "+exchangeName);

System.out.println("routingKey: "+routingKey);

System.out.println("Sent : "+message);

}

}

 

//读取队列消息

public static void main(String[] args) {

//test 

        try {  

            //队列名称 PropertiesUtil.QUEUE_NAME=queue_sina

            String queueName = PropertiesUtil.QUEUE_NAME;  

            ConnectionFactory factory = new ConnectionFactory();

           //PropertiesUtil.HOST = localhost

            factory.setHost(PropertiesUtil.HOST);

           //PropertiesUtil.USER=guest

            factory.setUsername(PropertiesUtil.USER);

           //PropertiesUtil.PASS=guest

            factory.setPassword(PropertiesUtil.PASS);

           //PropertiesUtil.PORT=5672

            factory.setPort(Integer.parseInt(PropertiesUtil.PORT));

            Connection conn = factory.newConnection();

            Channel channel = conn.createChannel();  

              

            channel.queueDeclare(queueName, true, false, false, null);  

              

            QueueingConsumer consumer = new QueueingConsumer(channel);  

            channel.basicConsume(queueName, true, consumer);  

              

            while(true) {  

                try {  

                    QueueingConsumer.Delivery delivery = consumer.nextDelivery();  

                    String message = new String(delivery.getBody());

                    System.out.println(" [x] Received '" + message + "'");

                } catch (ShutdownSignalException e) {  

                    e.printStackTrace();  

                } catch (InterruptedException e) {  

                    e.printStackTrace();  

                }  

            }  

              

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

}

相关文章
相关标签/搜索