java代码实现rabbitMQ请求

1、下载rabbitMQ的客户端,下载地址 http://www.rabbitmq.com/download.html

2、解压,并将后缀为.jar(可以不用包含junit.jar)的拷贝出来放入java的项目。

import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Send {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.11.188"); // mq ip
        factory.setPort(5672);// 端口号
        factory.setUsername("abc"); // rabbitMQ登录用户名
        factory.setPassword("abc123"); // rabbitMQ登录密码
        factory.setVirtualHost("hdware.adjust.host");
        Connection connection = null;
        connection = factory.newConnection();
        Channel channel = connection.createChannel();
        String sendStr = "需要发送的文本";
        // 发送
        channel.basicPublish("", "gz.hdwareinfo.queue", null,
                sendStr.getBytes("utf-8"));
        System.out.println("已发送" + sendStr);
        channel.close();
        connection.close();
    }
}

 

posted @ 2015-07-22 09:27  米蓝  阅读(1598)  评论(0编辑  收藏  举报