rabbitmq

生产

public class HelloRabbitMQProducerDemo {
	   private static String url = "192.168.10.230";
	    private static Integer port = 5672;
	    private static String username = "guest";
	    private static String password = "guest";
	 
	    public static void main(String[] args) throws IOException, TimeoutException {
	        ConnectionFactory connectionFactory = new ConnectionFactory();
	        connectionFactory.setHost(url);
	        connectionFactory.setPort(port);
	        connectionFactory.setUsername(username);
	        connectionFactory.setPassword(password);
	        Connection connection = connectionFactory.newConnection();
	        Channel channel = connection.createChannel();
	        //声明一个队列    queueDeclare第一个参数表示队列名称、第二个参数为是否持久化(true表示是,队列将在服务器重启时生存)、
	        // 第三个参数为是否是独占队列(创建者可以使用的私有队列,断开后自动删除)、第四个参数为当所有消费者客户端连接断开时是否自动删除队列、第五个参数为队列的其他参数
	        AMQP.Queue.DeclareOk declareOk = channel.queueDeclare("ice_pass_vehicle_track",false,false,false,null);
	        String message = "hello rabbitmq2";//消息内容
	        //发送消息到队列  第一个参数为交换机名称、第二个参数为队列映射的路由key、第三个参数为消息的其他属性、第四个参数为发送信息的主体
	        channel.basicPublish("SURVEY_CENTER","its_pass_vehicle",null,message.getBytes("UTF-8"));
	        System.out.println("producer send "+message);
	 
	        channel.close();
	        connection.close();
	    }
	 

 
}

 

 

消费


public class HelloRabbitMQConsumerDemo {
	   private static String url = "34.64.247.11";
	    private static Integer port = 5672;
	    private static String username = "guest";
	    private static String password = "guest";
	 
	    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
	        ConnectionFactory connectionFactory = new ConnectionFactory();
	        connectionFactory.setHost(url);
	        connectionFactory.setPort(port);
	        connectionFactory.setUsername(username);
	        connectionFactory.setPassword(password);
	        Connection connection = connectionFactory.newConnection();
	        Channel channel = connection.createChannel();
	        //声明关注的队列
	        AMQP.Queue.DeclareOk test = channel.queueDeclare("TESTPASS", true, false, false, null);
	        System.out.println("========consumer wait received message================");
	        //接收message方式一:
	        //DefaultConsumer类实现了Consumer接口,传入一个通道,如果通道中有消息,就会执行回调函数handleDelivery  delivery:传送,投递
	        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
	            @Override
	            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
	              
	            	String a =new String(body,"UTF-8");
	            	if (a.contains("34.64.242.253")){
		            	System.out.println(new String(body,"UTF-8"));

	            	}else {
	            		
	            	}
	            	
	            }
	        };
	        //自动回复队列应答 -- RabbitMQ中的消息确认机制
	        channel.basicConsume("TESTPASS", true, defaultConsumer);
	 


  //接收message方式二:
	        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);//QueueingConsumer extends DefaultConsumer
	        channel.basicConsume("ice_pass_vehicle_track",true,queueingConsumer);
	        while(true){//一直执行,获取Delivery传递对象
	            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
	            System.out.println(new String(delivery.getBody(),"UTF-8"));
	        }
	 
	     
	    }

 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值