List of usage examples for com.rabbitmq.client ConnectionFactory ConnectionFactory
ConnectionFactory
From source file:net.roboconf.messaging.rabbitmq.internal.RabbitMqClient.java
License:Apache License
@Override public void openConnection() throws IOException { // Already connected? Do nothing this.logger.info(getId() + " is opening a connection to RabbitMQ."); if (isConnected()) { this.logger.info(getId() + " has already a connection to RabbitMQ."); return;/*from w w w . j a v a 2 s .c om*/ } // Initialize the connection ConnectionFactory factory = new ConnectionFactory(); RabbitMqUtils.configureFactory(factory, this.configuration); this.channel = factory.newConnection().createChannel(); this.logger.info(getId() + " established a new connection with RabbitMQ. Channel # " + this.channel.getChannelNumber()); // Be notified when a message does not arrive in a queue (i.e. nobody is listening) this.channel.addReturnListener(new RoboconfReturnListener()); // Add a recoverable listener (when broken connections are recovered). // Given the way the RabbitMQ factory is configured, the channel should be "recoverable". ((Recoverable) this.channel).addRecoveryListener(new RoboconfRecoveryListener()); // Declare the exchanges. RabbitMqUtils.declareGlobalExchanges(this.domain, this.channel); RabbitMqUtils.declareApplicationExchanges(this.domain, this.applicationName, this.channel); // Declare the dedicated queue. String queueName = getQueueName(); this.channel.queueDeclare(queueName, true, false, true, null); // Start listening to messages. RoboconfConsumer consumer = new RoboconfConsumer(getId(), this.channel, this.messageQueue); consumer.handleConsumeOk(queueName); this.consumerTag = this.channel.basicConsume(queueName, true, consumer); this.logger.finer("A new consumer tag was created: " + this.consumerTag); }
From source file:net.roboconf.messaging.rabbitmq.internal.utils.RabbitMqTestUtils.java
License:Apache License
/** * Creates a channel to interact with a RabbitMQ server for tests. * @param messageServerIp the message server's IP address * @param username the user name for the messaging server * @param password the password for the messaging server * @return a non-null channel/*from w ww . j ava 2s.c o m*/ * @throws IOException if the creation failed */ public static Channel createTestChannel(String messageServerIp, String username, String password) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(messageServerIp); factory.setUsername(username); factory.setPassword(password); return factory.newConnection().createChannel(); }
From source file:net.roboconf.messaging.rabbitmq.internal.utils.RabbitMqUtilsTest.java
License:Apache License
@Test public void testConfigureFactory() throws Exception { String address = "http://roboconf.net/some/path"; int port = 18547; String username = "toto"; String password = "123456789"; ConnectionFactory factory = new ConnectionFactory(); Assert.assertNotSame(address, factory.getHost()); Assert.assertNotSame(port, factory.getPort()); Map<String, String> configuration = new HashMap<>(); configuration.put(RABBITMQ_SERVER_IP, "http://roboconf.net:" + port + "/some/path"); configuration.put(RABBITMQ_SERVER_USERNAME, username); configuration.put(RABBITMQ_SERVER_PASSWORD, password); RabbitMqUtils.configureFactory(factory, configuration); Assert.assertEquals(address, factory.getHost()); Assert.assertEquals(port, factory.getPort()); Assert.assertEquals(username, factory.getUsername()); Assert.assertEquals(password, factory.getPassword()); }
From source file:net.roboconf.messaging.rabbitmq.internal.utils.RabbitMqUtilsTest.java
License:Apache License
@Test public void testConfigureFactory_nullIp() throws Exception { ConnectionFactory factory = new ConnectionFactory(); String username = "toto"; String password = "123456789"; Map<String, String> configuration = new HashMap<>(); configuration.put(RABBITMQ_SERVER_IP, null); configuration.put(RABBITMQ_SERVER_USERNAME, username); configuration.put(RABBITMQ_SERVER_PASSWORD, password); RabbitMqUtils.configureFactory(factory, configuration); Assert.assertEquals(username, factory.getUsername()); Assert.assertEquals(password, factory.getPassword()); }
From source file:net.yacy.grid.io.messages.RabbitQueueFactory.java
License:Open Source License
private void init() throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.server); if (this.port > 0) factory.setPort(this.port); if (this.username != null && this.username.length() > 0) factory.setUsername(this.username); if (this.password != null && this.password.length() > 0) factory.setPassword(this.password); try {//w w w.ja v a 2 s . co m this.connection = factory.newConnection(); this.channel = connection.createChannel(); this.queues = new ConcurrentHashMap<>(); } catch (TimeoutException e) { throw new IOException(e.getMessage()); } }
From source file:nl.uva.sne.drip.api.rpc.DRIPCaller.java
License:Apache License
public DRIPCaller(String messageBrokerHost, String requestQeueName) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(messageBrokerHost);// w w w.j a va 2 s . c om factory.setPort(AMQP.PROTOCOL.PORT); //factory.setUsername("guest"); //factory.setPassword("pass"); connection = factory.newConnection(); channel = connection.createChannel(); // create a single callback queue per client not per requests. replyQueueName = channel.queueDeclare().getQueue(); this.requestQeueName = requestQeueName; this.mapper = new ObjectMapper(); mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); }
From source file:nl.uva.sne.drip.api.service.DRIPLogService.java
License:Apache License
public List<DRIPLogRecord> get() throws IOException, TimeoutException { Channel channel = null;/*from w w w . ja v a 2 s. co m*/ if (factory == null) { this.factory = new ConnectionFactory(); factory.setHost(messageBrokerHost); factory.setPort(AMQP.PROTOCOL.PORT); } if (this.mapper == null) { this.mapper = new ObjectMapper(); mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); } try (Connection connection = factory.newConnection()) { channel = connection.createChannel(); User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); String owner = user.getUsername(); String qeueNameUser = qeueName + "_" + owner; channel.queueDeclare(qeueNameUser, true, false, false, null); GetResponse response = channel.basicGet(qeueNameUser, true); List<DRIPLogRecord> logs = new ArrayList<>(); while (response != null) { String message = new String(response.getBody(), "UTF-8"); response = channel.basicGet(qeueNameUser, true); logs.add(mapper.readValue(message, DRIPLogRecord.class)); } return logs; } }
From source file:nl.uva.sne.drip.commons.utils.DRIPLogHandler.java
License:Apache License
public DRIPLogHandler(String messageBrokerHost) throws IOException, TimeoutException { factory = new ConnectionFactory(); factory.setHost(messageBrokerHost);/*ww w . jav a 2 s .c o m*/ factory.setPort(AMQP.PROTOCOL.PORT); //factory.setUsername("guest"); //factory.setPassword("pass"); this.qeueName = "log_qeue"; this.mapper = new ObjectMapper(); mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); }
From source file:nl.uva.sne.drip.drip.component_example.RPCServer.java
License:Apache License
private static void start() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST);/* w ww . j ava 2s .c o m*/ factory.setPassword("guest"); factory.setUsername("guest"); factory.setPort(AMQP.PROTOCOL.PORT); try (Connection connection = factory.newConnection()) { Channel channel = connection.createChannel(); //We define the queue name channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); //Set our own customized consummer Consumer c = new Consumer(channel); //Start listening for messages channel.basicConsume(RPC_QUEUE_NAME, false, c); //Block so we don't close the channel while (true) { try { Thread.sleep(100); } catch (InterruptedException _ignore) { } } } catch (IOException | TimeoutException ex) { Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:nl.uva.sne.drip.drip.provisioner.RPCServer.java
License:Apache License
private static void start() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(PropertyValues.HOST); factory.setPassword("guest"); factory.setUsername("guest"); factory.setPort(AMQP.PROTOCOL.PORT); Logger.getLogger(RPCServer.class.getName()).log(Level.INFO, "Connected to: {0}", PropertyValues.HOST); try (Connection connection = factory.newConnection()) { Channel channel = connection.createChannel(); //We define the queue name channel.queueDeclare(PropertyValues.RPC_QUEUE_NAME, false, false, false, null); DefaultConsumer c;//from w w w . j ava2 s . c o m if (PropertyValues.RPC_QUEUE_NAME.endsWith("v0")) { c = new nl.uva.sne.drip.drip.provisioner.v0.Consumer(channel); } else { c = new nl.uva.sne.drip.drip.provisioner.v1.Consumer(channel, PropertyValues.HOST); } //Start listening for messages channel.basicConsume(PropertyValues.RPC_QUEUE_NAME, false, c); //Block so we don't close the channel while (true) { try { Thread.sleep(100); } catch (InterruptedException _ignore) { } } } catch (IOException | TimeoutException ex) { Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex); } }