Database Reference
In-Depth Information
Creating a RabbitMQ feeder component
Now that we have installed the RabbitMQ cluster, all we need is to develop a publisher
component that will publish the messages to RabbitMQ. This will be a simple Java com-
ponent that will mimic the live feed to RabbitMQ. The basic code snippet for this is as fol-
lows:
public class FixedEmitter {
private static final String EXCHANGE_NAME = "MYExchange";
public static void main(String[] argv) throws Exception {
/*we are creating a new connection factory for builing
connections with exchange*/
ConnectionFactory factory = new ConnectionFactory();
/* we are specifying the RabbitMQ host address and port
here in */
Address[] addressArr = {
new Address("localhost", 5672)
}; //specify the IP if the queue is not on local node
where this program would execute
Connection connection =
factory.newConnection(addressArr);
//creating a channel for rabbitMQ
Channel channel = connection.createChannel();
//Declaring the queue and routing key
String queueName = "MYQueue";
String routingKey = "MYQueue";
//Declaring the Exchange
channel.exchangeDeclare(EXCHANGE_NAME, "direct", false);
Map < String, Object > args = new HashMap < String,
Object > ();
//defining the queue policy
args.put("x-ha-policy", "all");
//declaring and binding the queue to the exchange
channel.queueDeclare(queueName, true, false, false,
args);
channel.queueBind(queueName, EXCHANGE_NAME, routingKey);
String stoppedRecord;
Search WWH ::




Custom Search