Java tutorial
/* * Copyright (c) 2001-2018 GuaHao.com Corporation Limited. All rights reserved. * This software is the confidential and proprietary information of GuaHao Company. * ("Confidential Information"). * You shall not disclose such Confidential Information and shall use it only * in accordance with the terms of the license agreement you entered into with GuaHao.com. */ package com.github.hexsmith.rabbitmq.producer; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import java.io.IOException; import java.util.concurrent.TimeoutException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * ? * * @author yuzp * @version V1.0 * @since 2018-06-14 15:45 */ public class MessageProducer { private static final Logger logger = LoggerFactory.getLogger(MessageProducer.class); private static final String QUEUE_NAME = "hexsmith"; public boolean sendMessage(String message) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.7"); Connection connection = null; Channel channel = null; try { connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); logger.info("send message = {}", message); channel.close(); connection.close(); } catch (IOException | TimeoutException e) { logger.error("send message failed!,exception message is {}", e); return false; } return true; } public boolean sendMulitMessage(String message) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.7"); Connection connection = null; Channel channel = null; try { connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); int i = 0; int loop = 0; String originalMessage = message; while (loop < 10000) { loop++; message += i++; channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); logger.info("send message = {}", message); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } message = originalMessage; } channel.close(); connection.close(); } catch (IOException | TimeoutException e) { logger.error("send message failed!,exception message is {}", e); return false; } return true; } }