Java调用rabbitmq实际代码,java远程连接调用Rabbitmq的实例代码

本文介绍了java远程连接调用Rabbitmq,分享给大家,希望此文章对各位有所帮助。

打开IDEA创建一个maven工程(Java就可以了)。

9fae9e1efbf2080909f6f3a4a4c4042d.png

pom.xml文件如下

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.zhenqi

rabbitmq-study

1.0-SNAPSHOT

jar

rabbitmq-study

http://maven.apache.org

UTF-8

junit

junit

4.12

test

com.rabbitmq

amqp-client

4.1.0

org.slf4j

slf4j-api

org.slf4j

slf4j-log4j12

1.7.21

commons-lang

commons-lang

2.6

为了能远程访问rabbitmq,则需要编辑 /etc/rabbitmq/rabbitmq.conf,添加以下内容。

[

{rabbit, [{tcp_listeners, [5672]}, {loopback_users, ["asdf"]}]}

]

添加administrator角色

rabbitmqctl set_user_tags openstack administrator

创建抽象队列 EndPoint.java

package com.zhenqi;

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.ConnectionFactory;

/**

* Created by wuming on 2017/7/16.

*/

public abstract class EndPoint {

protected Channel channel;

protected Connection connection;

protected String endPointName;

public EndPoint(String endpointName) throws Exception {

this.endPointName = endpointName;

//创建一个连接工厂 connection factory

ConnectionFactory factory = new ConnectionFactory();

//设置rabbitmq-server服务IP地址

factory.setHost("192.168.146.128");

factory.setUsername("openstack");

factory.setPassword("rabbitmq");

factory.setPort(5672);

factory.setVirtualHost("/");

//得到 连接

connection = factory.newConnection();

//创建 channel实例

channel = connection.createChannel();

channel.queueDeclare(endpointName, false, false, false, null);

}

/**

* 关闭channel和connection。并非必须,因为隐含是自动调用的。

* @throws IOException

*/

public void close() throws Exception{

this.channel.close();

this.connection.close();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值