Java Code Examples for com.rabbitmq.client.Channel#basicPublish()
The following examples show how to use
com.rabbitmq.client.Channel#basicPublish() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Producer.java From rabbitmqexample with Apache License 2.0 | 7 votes |
public void SendResponse(String uuidrequest,String jsonResponse,String RoutingKey) { try { //System.out.println("Sending back the result"); Channel channel = connection.createChannel(); AMQP.BasicProperties.Builder bob = new AMQP.BasicProperties.Builder(); Map<String, Object> header = new HashMap<String,Object> (); header.put("uuidrequest", uuidrequest); bob.headers(header); // System.out.println("Request: " + guidRequest); channel.basicPublish(Constants.exchange, RoutingKey, bob.build(), jsonResponse.getBytes()); channel.close(); System.out.println(new Date()+ " -Done: messages sent: " + uuidrequest); } catch (Exception e) { e.printStackTrace(); } }
Example 2
Source File: RoutingSendDirect.java From rabbitmq with GNU General Public License v2.0 | 6 votes |
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // ���������� channel.exchangeDeclare(EXCHANGE_NAME, "direct"); // ������Ϣ for(String severity :routingKeys){ String message = "Send the message level:" + severity; channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println(" [x] Sent '" + severity + "':'" + message + "'"); } channel.close(); connection.close(); }
Example 3
Source File: AMQPWorkflowConsumer2.java From reactive with Creative Commons Zero v1.0 Universal | 6 votes |
public void execute() throws Exception { Channel channel = AMQPCommon.connect(); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume("trade.eq.q", true, consumer); QueueingConsumer.Delivery msg = null; while (true) { try { msg = consumer.nextDelivery(); String message = new String(msg.getBody()); System.out.println("received: " + message); String[] parts = message.split(","); long shares = new Long(parts[2]).longValue(); Thread.sleep(1000); } catch (Exception e) { System.out.println("error with trade: " + e.getMessage()); System.out.println("sending to workflow"); channel.basicPublish("", "workflow.q", null, msg.getBody()); } } }
Example 4
Source File: Producer.java From SpringBoot-Course with MIT License | 6 votes |
public static void main(String[] args) throws Exception { // 1. 创建链接工厂 ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); // 2. 通过链接工厂创建链接 Connection connection = connectionFactory.newConnection(); // 3. 通过链接创建通道(channel) Channel channel = connection.createChannel(); // 4. 通过 channel 发送数据 // exchange:交换机,如果不传默认为 AMQP default channel.basicPublish("", "helloworldQueue", null, "hello world".getBytes()); // 5. 关闭链接 channel.close(); connection.close(); }
Example 5
Source File: Worker.java From neo4j-mazerunner with Apache License 2.0 | 6 votes |
public static void sendMessage(String message) throws java.io.IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(ConfigurationLoader.getInstance().getRabbitmqNodename()); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); channel.basicPublish( "", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); }
Example 6
Source File: NewTask.java From rabbitmq with GNU General Public License v2.0 | 6 votes |
public static void main(String[] argv) throws java.io.IOException, Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); // �ַ���Ϣ for(int i = 0 ; i < 5; i++){ String message = "Hello World! " + i; channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); } channel.close(); connection.close(); }
Example 7
Source File: ConsumerTest.java From IGUANA with GNU Affero General Public License v3.0 | 6 votes |
public void send(Properties p) throws IOException, TimeoutException{ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(COMMON.CORE2RP_QUEUE_NAME, false, false, false, null); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(p); oos.flush(); oos.close(); bos.close(); byte [] data = bos.toByteArray(); channel.basicPublish("", COMMON.CORE2RP_QUEUE_NAME, null, data); }
Example 8
Source File: EmitLog.java From demo_springboot_rabbitmq with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { //建立连接工厂 ConnectionFactory factory = new ConnectionFactory(); //设置连接地址 factory.setHost("seaof-153-125-234-173.jp-tokyo-10.arukascloud.io"); factory.setPort(31084); //获取连接 Connection connection = factory.newConnection(); //获取渠道 Channel channel = connection.createChannel(); //声明交换机类型 channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); //产生随机数字 String message = RandomStringUtils.randomNumeric(8); channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes()); channel.close(); connection.close(); }
Example 9
Source File: ProducerExchangeConsumer_Direct.java From util4j with Apache License 2.0 | 5 votes |
public void producer() throws Exception{ //1、获取连接 Connection connection = RabbitMqConnectionFactoy.getConnection(); //2、声明信道 Channel channel = connection.createChannel(); //3、声明(创建)交换机 channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT); //4、定义消息内容(发布多条消息) Thread.sleep(3000); for(int i = 0 ; i < 6 ; i++){ String message = "hello rabbitmq "+i; //定义消息头 Map<String,Object> header=new HashMap<>(); header.put("i",i); BasicProperties bp=new BasicProperties.Builder().headers(header).build(); //5、发布消息 if(i%2!=0) {//单数进key1的队列 channel.basicPublish(EXCHANGE_NAME,ROUTER_KEY_1,bp,message.getBytes()); }else {//偶数进key2的队列 channel.basicPublish(EXCHANGE_NAME,ROUTER_KEY_2,bp,message.getBytes()); channel.basicPublish(EXCHANGE_NAME,ROUTER_KEY_22,bp,message.getBytes()); } System.out.println("[x] Sent'"+message+"'"); //模拟发送消息延时,便于演示多个消费者竞争接受消息 // Thread.sleep(i*10); } //6、关闭通道 channel.close(); //7、关闭连接 connection.close(); }
Example 10
Source File: Producer.java From SpringBoot-Course with MIT License | 5 votes |
public static void main(String[] args) throws Exception { // 创建链接工厂 ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); // 通过链接工厂创建链接 Connection connection = connectionFactory.newConnection(); // 通过链接创建通道(channel) Channel channel = connection.createChannel(); // 通过 channel 发送数据 // exchange:交换机,如果不传默认为 AMQP default String dlxExchangeName = "dlx_exchange_name"; String dlxRoutingKey = "dlx_routingKey"; String dlxMsg = "dlx hello world"; AMQP.BasicProperties basicProperties = new AMQP.BasicProperties.Builder() .deliveryMode(2) .contentEncoding("UTF-8") .expiration("5000") // 设置 5 秒中过期 .build(); channel.basicPublish(dlxExchangeName, dlxRoutingKey, basicProperties, dlxMsg.getBytes()); // 关闭链接 channel.close(); connection.close(); }
Example 11
Source File: NegativeQueueDeleteTest.java From ballerina-message-broker with Apache License 2.0 | 5 votes |
@Parameters({"broker-hostname", "broker-port", "admin-username", "admin-password"}) @BeforeMethod public void setUp(String hostname, String port, String username, String password) throws Exception { amqpConnection = ClientHelper.getAmqpConnection(username, password, hostname, port); Channel channel = amqpConnection.createChannel(); channel.queueDeclare(queueWithConsumers, false, false, false, null); channel.queueDeclare(queueWithMessages, false, false, false, null); channel.basicConsume(queueWithConsumers, new DefaultConsumer(channel)); channel.basicPublish("<<default>>", queueWithMessages, new AMQP.BasicProperties(), "Test Message".getBytes()); }
Example 12
Source File: TopicSend.java From rabbitmq with GNU General Public License v2.0 | 5 votes |
public static void main(String[] argv) { Connection connection = null; Channel channel = null; try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); // ����һ��ƥ��ģʽ�Ľ����� channel.exchangeDeclare(EXCHANGE_NAME, "topic"); // �����͵���Ϣ String[] routingKeys = new String[]{"quick.orange.rabbit", "lazy.orange.elephant", "quick.orange.fox", "lazy.brown.fox", "quick.brown.fox", "quick.orange.male.rabbit", "lazy.orange.male.rabbit"}; // ������Ϣ for(String severity :routingKeys){ String message = "From "+severity+" routingKey' s message!"; channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println("TopicSend [x] Sent '" + severity + "':'" + message + "'"); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
Example 13
Source File: Producer.java From SpringBoot-Course with MIT License | 5 votes |
public static void main(String[] args) throws Exception { // 创建链接工厂 ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); // 通过链接工厂创建链接 Connection connection = connectionFactory.newConnection(); // 通过链接创建通道(channel) Channel channel = connection.createChannel(); // 通过 channel 发送数据 // exchange:交换机,如果不传默认为 AMQP default String topicExchangeName = "topic_exchange_name"; String topicRoutingKey1 = "topic_routingKey.test1"; String topicRoutingKey2 = "topic_routingKey.test2"; String topicRoutingKey3 = "topic_routingKey.test3.test03"; String topicMsg = "topic hello world"; channel.basicPublish(topicExchangeName, topicRoutingKey1, null, topicMsg.getBytes()); channel.basicPublish(topicExchangeName, topicRoutingKey2, null, topicMsg.getBytes()); channel.basicPublish(topicExchangeName, topicRoutingKey3, null, topicMsg.getBytes()); // 关闭链接 channel.close(); connection.close(); }
Example 14
Source File: NewTask.java From demo_springboot_rabbitmq with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException, TimeoutException { //建立连接工厂 ConnectionFactory factory = new ConnectionFactory(); //设置连接地址 factory.setHost("seaof-153-125-234-173.jp-tokyo-10.arukascloud.io"); factory.setPort(31084); //获取连接 Connection connection = factory.newConnection(); //获取渠道 Channel channel = connection.createChannel(); //声明队列,如果不存在就新建 //参数1队列名称;参数2是否持久化;参数3排他性队列,连接断开自动删除;参数4是否自动删除;参数5.参数 channel.queueDeclare(QUEUE_NAME, false, false, false, null); //发送的消息 for (int i = 0; i < 10; i++) { String message = "Hello"; int num = ThreadLocalRandom.current().nextInt(10); String append = ""; for (int j = 0; j < num; j++) { append = append + "."; } message = message + (append); //参数1 交换机;参数2 路由键;参数3 基础属性,(持久化方式);参数4 消息体 channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes()); System.out.println(Thread.currentThread().getName() + "[send]" + message); } channel.close(); connection.close(); }
Example 15
Source File: RabbitIntegrationTest.java From brooklyn-library with Apache License 2.0 | 5 votes |
/** * Test that an AMQP client can connect to and use the broker. */ @Test(groups = {"Integration", "WIP"}) public void testClientConnection() throws Exception { rabbit = app.createAndManageChild(EntitySpec.create(RabbitBroker.class)); rabbit.start(ImmutableList.of(testLocation)); EntityAsserts.assertAttributeEqualsEventually(rabbit, Startable.SERVICE_UP, true); byte[] content = "MessageBody".getBytes(Charsets.UTF_8); String queue = "queueName"; Channel producer = null; Channel consumer = null; try { producer = getAmqpChannel(rabbit); consumer = getAmqpChannel(rabbit); producer.queueDeclare(queue, true, false, false, ImmutableMap.<String,Object>of()); producer.queueBind(queue, AmqpExchange.DIRECT, queue); producer.basicPublish(AmqpExchange.DIRECT, queue, null, content); QueueingConsumer queueConsumer = new QueueingConsumer(consumer); consumer.basicConsume(queue, true, queueConsumer); QueueingConsumer.Delivery delivery = queueConsumer.nextDelivery(60 * 1000l); // one minute timeout assertEquals(delivery.getBody(), content); } finally { closeSafely(producer, 10*1000); closeSafely(consumer, 10*1000); } }
Example 16
Source File: Emitter.java From helix with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { if (args.length < 1) { System.err .println("USAGE: java Emitter rabbitmqServer (e.g. localhost) numberOfMessage (optional)"); System.exit(1); } final String mqServer = args[0]; // "zzhang-ld"; int count = Integer.MAX_VALUE; if (args.length > 1) { try { count = Integer.parseInt(args[1]); } catch (Exception e) { // TODO: handle exception } } System.out.println("Sending " + count + " messages with random topic id"); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(mqServer); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); for (int i = 0; i < count; i++) { int rand = ((int) (Math.random() * 10000) % SetupConsumerCluster.DEFAULT_PARTITION_NUMBER); String routingKey = "topic_" + rand; String message = "message_" + rand; channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes()); System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'"); Thread.sleep(1000); } connection.close(); }
Example 17
Source File: Producer.java From code with Apache License 2.0 | 5 votes |
/** * 1.消息被拒绝(basic.reject/ basic.nack)并且requeue=false(没有重回队列) * 2.消息TTL过期 * 3.队列达到最大长度 */ public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost(Constant.ip); connectionFactory.setPort(Constant.port); connectionFactory.setVirtualHost("/"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); String exchange = "test_dlx_exchange"; String routingKey = "dlx.save"; String msg = "Hello RabbitMQ DLX Message"; for(int i =0; i<1; i ++){ AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder() .deliveryMode(2) .contentEncoding("UTF-8") .expiration("10000") .build(); channel.basicPublish(exchange, routingKey, true, properties, msg.getBytes()); } }
Example 18
Source File: MainControllerTest.java From IGUANA with GNU Affero General Public License v3.0 | 5 votes |
public static void send(Channel channel, Properties p) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(p); oos.flush(); oos.close(); bos.close(); byte[] data = bos.toByteArray(); channel.basicPublish("", COMMON.CORE2RP_QUEUE_NAME, null, data); }
Example 19
Source File: P.java From rabbitmq with GNU General Public License v2.0 | 5 votes |
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Hello World!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println("P [x] Sent '" + message + "'"); channel.close(); connection.close(); }
Example 20
Source File: ExchangeSender.java From eip with MIT License | 4 votes |
private static void sendMessage(Channel channel, String exchange, String key, String message) throws IOException { channel.basicPublish(exchange, key, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); }