JMS是实现中间件RabbitMQ

所需要的的依赖jar包  

 <dependency>

     <groupId>com.rabbitmq</groupId>

    <artifactId>amqp-client</artifactId>

    <version></version>

</dependency>

示例: 

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

/**
 * RabbitMQ 消费者
 * @author YeDong
 *
 */
public class QueueConsumerRabbitMQ {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //获取连接
        Connection conn = connectionFactory.newConnection();
        //设置用户名
        connectionFactory.setUsername("guest");
        //设置密码
        connectionFactory.setPassword("guest");
        //设置主机
        connectionFactory.setHost("localhost");
        //设置虚拟机
        connectionFactory.setVirtualHost("/");
        //创建信道
        final Channel channel1 = conn.createChannel();
        //声明交换器
        String exchangeName = "hello-RabbitMQ";
        channel1.exchangeDeclare(exchangeName,"direct",true);
        //从信道里面取出来队列的名称
        String queueName = channel1.queueDeclare().getQueue();    
        //通过路由主键 把   设置路由主键 绑定到交换器
        String routingKey = "routingKey";
        channel1.queueBind(queueName,exchangeName,routingKey);
        /**
         * 同一个会话, consumerTag 是固定的 可以做此会话的名字, deliveryTag 每次接收消息+1,可以做此消息处理通道的名字。
         */
        while(true) {
            //设置是否开启自动回执模式
            boolean autoAck = false;
            //
            String consumerTag="";
            //队列名称   是否自动回执   ??? // 消费者类
            channel1.basicConsume(queueName,autoAck,consumerTag, new DefaultConsumer(channel1){
                public void handleDelivery(String consumerTag, Envelope envelope, 
                        BasicProperties properties, byte[] messageBody)
                        throws IOException {
                    String routingKey = envelope.getRoutingKey();
                    String contentType = properties.getContentType();
                    System.out.println("消费者的路由主键:"+routingKey);
                    System.out.println("消费者的内容类型:"+contentType);
                    long deliverTay = envelope.getDeliveryTag();
                    //确认消息
                    channel1.basicAck(deliverTay, false);
                    
                    Date date = new Date();
                    SimpleDateFormat sf = new SimpleDateFormat("HH-MM-SS");
                    
                    System.out.println(sf.format(date)+"接收到==:"+"消息的具体内容是:"+ new String(messageBody,"utf-8"));
                }
            });
        }
    }
}

=============================================================================================

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * RabbitMQ 消息生产者2 
 * @author YeDong
 *
 */
public class QueueProducerRabbitMQ {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //获取连接
        Connection conn = connectionFactory.newConnection();
        //设置用户名
        connectionFactory.setUsername("guest");
        //设置密码
        connectionFactory.setPassword("guest");
        //设置连接主机
        connectionFactory.setHost("localhost");
        //设置虚拟机路径
        connectionFactory.setVirtualHost("/");
        //创建通道
        Channel channel = conn.createChannel();
        //声明交换器
        String exchangeName = "hello-RabbitMQ";
        
        channel.exchangeDeclare(exchangeName, "direct", true);
        //约定路由主键
        String routingKey = "routingKey";
        
        Date date = new Date();
        
        SimpleDateFormat format = new SimpleDateFormat("HH:MM:SS");
        
        String str = format.format(date);
        
        //定义消息具体的内容 :body
        byte [] messageBody = str.getBytes();
        //发送消息
        channel.basicPublish(exchangeName, routingKey, null, messageBody);// 交换器   路由主键  ?? 消息实体
    
        //关闭信道
        channel.close();
        conn.close();
    }
}    

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值