com.siva.rabbitmq.AmqpMsgPublisher.java Source code

Java tutorial

Introduction

Here is the source code for com.siva.rabbitmq.AmqpMsgPublisher.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.siva.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 *
 * @author siva
 */
public class AmqpMsgPublisher {

    private static final String EXCHANGE_NAME = "amq.topic";

    public static void main(String[] argv) throws Exception {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setUsername("admin");
        factory.setPassword("welcome01");
        factory.setPort(5672);
        factory.setVirtualHost("/admin_vhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "topic", true);

        String routingKey = "mqtt_topic.iot.admin_vhost";
        String message = "Test Message from IoT";

        channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
        System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");

        connection.close();
    }

}