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();  

        }  

}

相關文章
相關標籤/搜索