配置文件
rabbitMQ: host: rabbitmq.com port: 30000 username: xxxx password: xxxxx topic: amq.topic queueName: icost-beta virtualHost: icost-beta #开启发送确认机制,将来消息到达交换机以后有一个回调 publisher-confirm-type: correlated #消息到达消息队列回调(如果消息没有成功到达队列,会触发回调方法) publisher-returns: true template: retry: enabled: true # 开启重发机制 initial-interval: 1000ms #间隔 1秒 max-attempts: 6 #最多发6次 multiplier: 1.2 #每次间隔 时间*1.2 max-interval: 10000ms #每次最大间隔时间 listener: simple: acknowledge-mode: manual
读取配置
@Value("${rabbitMQ.username}") private String username; @Value("${rabbitMQ.password}") private String password; @Value("${rabbitMQ.host}") private String host; @Value("${rabbitMQ.port}") private int port; @Value("${rabbitMQ.topic}") private String topic; //声明队列 @Value("${rabbitMQ.queueName}") private String queueName; @Value("${rabbitMQ.virtualHost}") private String virtualHost;
发送消息
/** * 发布信息 */ private void sendProducer (MQMessage mqMessage)throws IOException, TimeoutException{ Gson gson = new Gson(); // 发送同步消息 String messageStr = gson.toJson(mqMessage); //创建连接工厂 ConnectionFactory factory = new ConnectionFactory(); factory.setUsername(username); factory.setPassword(password); //设置 RabbitMQ 地址 factory.setHost(host); factory.setPort(port); factory.setVirtualHost(virtualHost); //建立到代理服务器到连接 Connection conn = factory.newConnection(); //获得信道 Channel channel = conn.createChannel(); //声明交换机 channel.exchangeDeclare(topic, "topic",true); //消息持久化 channel.queueDeclare(queueName,true,false,false,null); String domainType =null; if("ContractCreate".equals(mqMessage.getDomainType())){ domainType = "Contract."+mqMessage.getDomainType(); }else if("ContractModification".equals(mqMessage.getDomainType())){ domainType = "Contract."+mqMessage.getDomainType(); }else if("ContractPayment".equals(mqMessage.getDomainType())){ domainType = "Contract."+mqMessage.getDomainType(); }else if("ContractSettlement".equals(mqMessage.getDomainType())){ domainType = "Contract."+mqMessage.getDomainType(); }else if("EngineerInstruction".equals(mqMessage.getDomainType())){ domainType = "Contract."+mqMessage.getDomainType(); }else if("JicaiContract".equals(mqMessage.getDomainType())){ domainType = "Contract."+mqMessage.getDomainType(); }else if("bid".equals(mqMessage.getDomainType())){ domainType = "bid."+mqMessage.getDomainType(); }else if("vendor".equals(mqMessage.getDomainType())){ domainType = "vendor."+mqMessage.getDomainType(); }else if("SubProject".equals(mqMessage.getDomainType())){ domainType = "SubProject."+mqMessage.getDomainType(); }else if("Project".equals(mqMessage.getDomainType())){ domainType = "SubProject."+mqMessage.getDomainType(); }else if("ContractTemplate".equals(mqMessage.getDomainType())){ domainType = "TargetCost."+mqMessage.getDomainType(); }else if("ContractPlanning".equals(mqMessage.getDomainType())){ domainType = "TargetCost."+mqMessage.getDomainType(); }else if("TargetCost".equals(mqMessage.getDomainType())){ domainType = "TargetCost."+mqMessage.getDomainType(); }else{ domainType = mqMessage.getDomainType(); } //发布消息==>使用动态路由(通配符方式) String key = domainType; //指定发布的路由key ContractCreate||ContractModification||ContractPayment||ContractSettlement||EngineerInstruction||JicaiContract channel.basicPublish(topic, key, true,null,messageStr.getBytes(StandardCharsets.UTF_8)); // 同步发送消息,只要不抛异常就是成功。 log.info(new Date() + " Send mq message success:--"+messageStr.toString()); channel.close(); conn.close(); }
接收消息
/** * 订阅消息,处理业务 */ public void normalSubscribe() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setUsername(username); factory.setPassword(password); //设置 RabbitMQ 地址 factory.setHost(host); factory.setPort(port); factory.setVirtualHost(virtualHost); //建立到代理服务器到连接 Connection conn = factory.newConnection(); //获得信道 final Channel channel = conn.createChannel(); //声明交换机 channel.exchangeDeclare(topic, "topic",true); //绑定临时队列与交换机并设置获取交换机中动态路由 String key = "bid.*";//使用通配符指定路由key channel.queueBind(queueName, topic, key); //消费消息 channel.basicConsume(queueName, true, new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { try { //接收到的消息内容 String msg = new String(body); MQWorkflowMessageBody messageBody = objectMapper.readValue(msg, MQWorkflowMessageBody.class); if ("bid".equals(messageBody.getDomainType()) && StringUtils.isNotBlank(messageBody.getBusinessKey()) && StringUtils.isNotBlank(String.valueOf(messageBody.getStatus()))) { handler.processRequest(messageBody.getBusinessKey(), String.valueOf(messageBody.getStatus())); } } catch (Exception e) { e.printStackTrace(); } } }); }
学不学,不学就穷死!!!