生产者:直连模式
package com.huixiang.rabbitmq.simple;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Productor {
public static void main(String[] args) {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("");
connectionFactory.setPort(5672);
connectionFactory.setUsername("");
connectionFactory.setPassword("");
connectionFactory.setVirtualHost("/");
Connection connection = null;
Channel channel = null;
try {
connection = connectionFactory.newConnection("生产者");
channel = connection.createChannel();
String queueName = "queue1";
channel.queueDeclare(queueName,false,false,false,null);
String info = "nihao !";
channel.basicPublish("",queueName,null,info.getBytes());
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
消费者:直连
package com.huixiang.rabbitmq.simple;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Consumer {
public static void main(String[] args) {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("");
connectionFactory.setPort(5672);
connectionFactory.setUsername("");
connectionFactory.setPassword("");
connectionFactory.setVirtualHost("/");
Connection connection = null;
Channel channel = null;
try {
connection = connectionFactory.newConnection("生产者");
channel = connection.createChannel();
channel.queueDeclare("queue1",false,false,false,null);
channel.basicConsume("queue1", true, new DeliverCallback() {
@Override
public void handle(String consumerTag, Delivery message) throws IOException {
System.out.println("收到消息" + new String(message.getBody(), "UTF-8"));
}
}, new CancelCallback() {
@Override
public void handle(String consumerTag) throws IOException {
System.out.println("失败");
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
代码冗余,提取工具类
package com.huixiang.utils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class RabbitmqUtils {
private static ConnectionFactory connectionFactory;
static {
connectionFactory = new ConnectionFactory();
connectionFactory.setHost("");
connectionFactory.setPort(5672);
connectionFactory.setUsername("");
connectionFactory.setPassword("");
connectionFactory.setVirtualHost("/");
}
public static Connection setConnection(){
try {
return connectionFactory.newConnection();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void closeConnectionChanel(Channel channel,Connection connection) {
try {
if (channel != null) channel.close();
if (connection != null) connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
connection = RabbitmqUtils.setConnection();
channel = connection.createChannel();
String queueName = "queue1";
channel.queueDeclare(queueName,false,false,false,null);
String info = "nihao !";
channel.basicPublish("",queueName,null,info.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
RabbitmqUtils.closeConnectionChanel(channel,connection);
}
}