Example usage for com.rabbitmq.client ConnectionFactory ConnectionFactory

List of usage examples for com.rabbitmq.client ConnectionFactory ConnectionFactory

Introduction

In this page you can find the example usage for com.rabbitmq.client ConnectionFactory ConnectionFactory.

Prototype

ConnectionFactory

Source Link

Usage

From source file:es.devcircus.rabbitmq_examples.rabbitmq_work_queues.Worker.java

License:Open Source License

/**
 * /*from w  ww. j a v  a  2 s.  c  o  m*/
 * @param argv
 * @throws Exception 
 */
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(TASK_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    channel.basicQos(1);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(TASK_QUEUE_NAME, false, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());

        System.out.println(" [x] Received '" + message + "'");
        doWork(message);
        System.out.println(" [x] Done");

        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}

From source file:eu.betaas.rabbitmq.publisher.PublisherService.java

License:Apache License

public void startService() {

    if (!enabled) {
        log.info("#BUS not enabled #");
        return;/*ww w.  j av  a 2 s  .co m*/
    }
    log.info("#BUS enabled: #");
    factory = new ConnectionFactory();
    factory.setHost(host);
    log.info("#Connected #");
    try {

        connection = factory.newConnection();

        channel = connection.createChannel();

        channel.exchangeDeclare(ename, mode);
        log.info("#Exchange " + ename + " declared as " + mode + " #");

    } catch (IOException e) {
        enabled = false;
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:eu.betaas.rabbitmq.subscriber.SubscriberService.java

License:Apache License

public void startService() {
    try {//  w w w  .  j  a  v  a  2s .com
        log.info("#Starting queue #");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection;
        log.info("#Starting #");
        connection = factory.newConnection();
        log.info("#Starting connection#");
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(ename, mode);
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, ename, bussubkey);
        log.info("#Starting connection on queue #");
        consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, consumer);

        log.info("#Running #");
        run();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ShutdownSignalException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ConsumerCancelledException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:eu.eco2clouds.accounting.monitoringcollector.MMQMonitor.java

License:Apache License

public MMQMonitor() {
    factory = new ConnectionFactory();
    factory.setHost(ConfigurationValues.mmqHost);
    factory.setVirtualHost(VIRTUAL_HOST);
    factory.setUsername(ConfigurationValues.mmqUsername);
    factory.setPassword(ConfigurationValues.mmqPassword);
}

From source file:eu.europeana.servicebus.client.rabbitmq.RabbitMQClientAsync.java

/**
 * RabbitMQ implementation of ESBClient/*ww  w . jav a2 s  . c  om*/
 *
 * @param host The host to connect to
 * @param incomingQueue The incoming queue
 * @param outgoingQueue The outgoing queue
 * @param username Username
 * @param password Password
 * @param consumer The consumer implementation - It can be null. It allows asynchronous consumers as well as enables
 * custom behaviour handling upon incoming message. The default message handling is and should be agnostic of the
 * method semantics to be implemented
 * @throws IOException
 */
public RabbitMQClientAsync(String host, String incomingQueue, String outgoingQueue, String username,
        String password, Consumer consumer) throws IOException {
    this.host = host;
    this.incomingQueue = incomingQueue;
    this.outgoingQueue = outgoingQueue;
    this.username = username;
    this.password = password;
    if (consumer == null) {
        this.consumer = new DefaultConsumer(receiveChannel);
    } else {
        this.consumer = consumer;
    }
    ConnectionFactory factory = new ConnectionFactory();
    builder = new AMQP.BasicProperties.Builder();
    factory.setHost(host);
    factory.setUsername(username);
    factory.setPassword(password);

    connection = factory.newConnection();
    sendChannel = connection.createChannel();
    receiveChannel = connection.createChannel();
    sendChannel.queueDeclare(outgoingQueue, true, false, false, null);
    receiveChannel.queueDeclare(incomingQueue, true, false, false, null);
    receiveChannel.basicConsume(incomingQueue, true, consumer);

}

From source file:eu.netide.util.topology.update.impl.NotificationProducer.java

License:Open Source License

public void init(String rabbitHost, int rabbitPort, String rabbitUser, String rabbitPassword,
        String rabbitVirtualHost, String exchangeName, String baseTopicName, String nodeTopicName,
        String nodeConnectorTopicName, String linkTopicName) {

    this.exchangeName = exchangeName;
    this.baseTopicName = baseTopicName;
    this.nodeTopicName = nodeTopicName;
    this.nodeConnectorTopicName = nodeConnectorTopicName;
    this.linkTopicName = linkTopicName;

    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(rabbitUser);// w w  w  .j a  v  a 2 s.  co  m
    factory.setPassword(rabbitPassword);
    factory.setVirtualHost(rabbitVirtualHost);
    factory.setHost(rabbitHost);
    factory.setPort(rabbitPort);
    factory.setAutomaticRecoveryEnabled(true);

    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare(exchangeName, "topic", true);
        init = true;
    } catch (IOException e) {
        LOG.error(e.getMessage());
    } catch (TimeoutException e) {
        LOG.error(e.getMessage());
    }
}

From source file:FilterWheelHWAdapter.FilterWheelHWApadpterImplAbstract.java

License:Open Source License

public FilterWheelHWApadpterImplAbstract() {

    mLogger = Logger.getLogger("FilterWheelHWAdapter");
    PropertyConfigurator.configure("log4j.properties");

    try {/*from   w  ww.  j  a va  2 s. c om*/
        mFactory = new ConnectionFactory();
        mFactory.setHost("localhost");
        mConnection = mFactory.newConnection();
        mChannel = mConnection.createChannel();

        /*
         * Defines that messages in this queue are persistent (i.e. to be saved on disk)    
         * so that even if RabbitMQ server quits the messages is not lost.
         * Note that when publishing a message, the msg itself has to be declared persistent.
         */
        boolean durable = false; // for commands we don't want it!

        /*
         * true if the server should consider messages acknowledged once delivered; 
         * false if the server should expect explicit acknowledgements.
         */
        boolean autoAck = true;

        // command queue      
        boolean exclusive = false; // restricted to this connection
        boolean autoDelete = false; // server will delete the queue when no longer in use
        mChannel.queueDeclare(CMD_QUEUE_NAME, durable, exclusive, autoDelete, null);

        // broadcasted commands (don't need to subscribe to a topic)
        mChannel.exchangeDeclare(CMD_EXCHANGE_NAME, "fanout", durable);
        String cmdFanoutQueueName = mChannel.queueDeclare().getQueue();
        mChannel.queueBind(cmdFanoutQueueName, CMD_EXCHANGE_NAME, "");

        // data topics to subscribe
        durable = true; // for data we want persistance!
        mChannel.exchangeDeclare(DATA_EXCHANGE_NAME, "topic", durable);
        String dataPubsubQueueName = mChannel.queueDeclare().getQueue();
        mChannel.queueBind(dataPubsubQueueName, DATA_EXCHANGE_NAME, "SetRefPos");

        // define which queues to get the messages from 
        mConsumer = new QueueingConsumer(mChannel);
        mChannel.basicConsume(CMD_QUEUE_NAME, autoAck, mConsumer);
        mChannel.basicConsume(cmdFanoutQueueName, autoAck, mConsumer);
        mChannel.basicConsume(dataPubsubQueueName, autoAck, mConsumer);

    } catch (IOException e) {
        mLogger.error("Could not create RMQ channel: " + e.getMessage());
        return;
    }

    try {
        mEngine = new SMEngine(mLogger, SMJavaInvoker.class);
        // The context has to be created before the actions/activities are executed
        mEngine.setContextVar("CHANNEL", mChannel);
        mEngine.setContextVar("CMD_QUEUE_NAME", CMD_QUEUE_NAME);
        mEngine.setContextVar("CMD_EXCHANGE_NAME", CMD_EXCHANGE_NAME);
        mEngine.setContextVar("DATA_EXCHANGE_NAME", DATA_EXCHANGE_NAME);
        mEngine.loadModel("FilterWheelHWAdapter.xml");
        mEngine.startExecution();
    } catch (IOException e) {
        mLogger.error("Could not start SM execution: " + e.getMessage());
        return;
    }

    mIsRunning = true;
}

From source file:FilterWheelHWAdapter.SMActivity.java

License:Open Source License

public void openConnection() {
    try {/*from w w  w . j  av a2 s .co  m*/
        boolean durable = true; // for data we want persistance
        boolean autoAck = true;

        mFactory = new ConnectionFactory();
        mFactory.setHost("localhost");
        mConnection = mFactory.newConnection();
        mChannel = mConnection.createChannel();

        // data pub/sub
        mChannel.exchangeDeclare(DATA_EXCHANGE_NAME, "topic", durable);
        String pubsubQueueName = mChannel.queueDeclare().getQueue();

        mChannel.queueBind(pubsubQueueName, DATA_EXCHANGE_NAME, "SetRefPos");

        mConsumer = new QueueingConsumer(mChannel);
        mChannel.basicConsume(pubsubQueueName, autoAck, mConsumer);

    } catch (IOException e) {
        getLogger().error("Could not create RMQ channel: " + e.getMessage());
        stopRunning();
    }
}

From source file:flens.Agent.java

License:Apache License

public static void main(String[] args) throws IOException {

    ConfigHandler ch = new ConfigHandler();

    Gson g = new Gson();

    Map<String, Object> myconfig = g.fromJson(new FileReader(args[0]), HashMap.class);

    Map<String, String> tags = (Map<String, String>) myconfig.get("tags");

    Map<String, Object> initial = (Map<String, Object>) myconfig.get("init");

    String server = (String) myconfig.get("server");
    String name = (String) myconfig.get("name");

    System.out.println(String.format("connecting to %s as %s, with tags %s", server, name, tags));

    List<CommandHandler> chs = new LinkedList<>();
    chs.add(new PingHandler());
    chs.add(ch);/*from   w w  w . ja va 2s .co  m*/

    ConnectionFactory c = new ConnectionFactory();
    c.setHost(server);
    c.setUsername("guest");
    c.setPassword("guest");
    c.setPort(5672);

    CommandServer cs = new CommandServer(name, c.newConnection(), tags, chs);
    cs.start();
    ch.load(initial);
    ch.getEngine().addTags(tags);
    ch.getEngine().start();
}

From source file:flens.input.AMQPInput.java

License:Apache License

public AMQPInput(String name, Tagger tagger, String host, int port, String vhost, String user, String pass,
        String exchange, String queue, String routingkey) {
    super(name, tagger);

    factory = new ConnectionFactory();
    factory.setHost(host);/*from   www  .j a  v  a  2  s .  com*/
    factory.setPort(port);
    factory.setUsername(user);
    factory.setPassword(pass);
    if (vhost != null)
        factory.setVirtualHost(vhost);

    this.queue = queue;
    this.exchange = exchange;
    this.key = routingkey;
}