作者 朱兆平

增加rabitMq处理工具类

... ... @@ -35,6 +35,19 @@
<artifactId>annotations</artifactId>
<version>RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
... ...
package com.tianbo.util.RabitMq;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* 建立mq链接
*/
public class ConnectionUtil {
/**
* 链接MQ
* @param hostIp mq服务器Ip地址
* @param hostPort mq服务器端口号
* @param vHostName VirtualHost名称
* @param userName 登录账号
* @param password 登录密码
* @return 返回链接
* @throws Exception
*/
public static Connection getConnection(String hostIp,int hostPort,String vHostName,String userName,String password) throws Exception {
//定义连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置服务地址
factory.setHost(hostIp);
//端口
factory.setPort(hostPort);
//设置账号信息,用户名、密码、vhost
factory.setVirtualHost(vHostName);
factory.setUsername(userName);
factory.setPassword(password);
// 通过工程获取连接
Connection connection = factory.newConnection();
return connection;
}
}
... ...
package com.tianbo.util.RabitMq;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.nio.charset.StandardCharsets;
/**
* 发送 [内容] 到队列
*/
public class MQSendMsg {
/**
*
* @param queueName 队列名称
* @param msg 发送内容
* @return
*/
public static boolean sendMsg(String queueName,String msg,String hostIp,int hostPort,String vHostName,String userName,String password){
try{
// 获取到连接以及mq通道
Connection connection = ConnectionUtil.getConnection(hostIp,hostPort,vHostName,userName,password);
// 从连接中创建通道
Channel channel = connection.createChannel();
// String exchangeName = "amq.topic";
// String routingKey = "consumer.send";
// 声明(创建)队列
channel.queueDeclare(queueName, true, false, false, null);
// 消息内容
channel.basicPublish("", queueName, null, msg.getBytes(StandardCharsets.UTF_8));
System.out.println("消息发送成功>>>" + msg + "<<<");
//关闭通道和连接
channel.close();
connection.close();
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
}
... ...
package com.tianbo.util.RabitMq;
import com.rabbitmq.client.*;
import java.io.IOException;
public class MqGetMsg extends DefaultConsumer{
public MqGetMsg(Channel channel) {
super(channel);
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//TODO someting
System.err.println("-----------consume message----------");
System.err.println("consumerTag: " + consumerTag);
System.err.println("envelope: " + envelope);
System.err.println("properties: " + properties);
System.err.println("body: " + new String(body));
}
}
... ...
package com.tianbo.util.RabitMq;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Envelope;
public class MqResponse {
private String consumerTag;
private Envelope envelope;
private AMQP.BasicProperties properties;
private String content;
public MqResponse(){
}
public MqResponse(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, String content) {
this.consumerTag = consumerTag;
this.envelope = envelope;
this.properties = properties;
this.content = content;
}
public String getConsumerTag() {
return consumerTag;
}
public void setConsumerTag(String consumerTag) {
this.consumerTag = consumerTag;
}
public Envelope getEnvelope() {
return envelope;
}
public void setEnvelope(Envelope envelope) {
this.envelope = envelope;
}
public AMQP.BasicProperties getProperties() {
return properties;
}
public void setProperties(AMQP.BasicProperties properties) {
this.properties = properties;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
... ...