List of usage examples for com.rabbitmq.client ConnectionFactory setHost
public void setHost(String host)
From source file:net.lshift.camcapture.AMQPacketProducer.java
License:Open Source License
public AMQPacketProducer(String host, String exchange, String routingKey) throws IOException { this.host = host; this.exchange = exchange; this.routingKey = routingKey; ConnectionFactory cf = new ConnectionFactory(); cf.setHost(host); this.conn = cf.newConnection(); this.ch = conn.createChannel(); ch.exchangeDeclare(exchange, "fanout"); }
From source file:net.lshift.camdisplay.Main.java
License:Open Source License
public Main(String host, String exch, String nickname, final String mixerSpec) throws IOException { frame = new JFrame("RabbitCam: " + host + "/" + exch); panel = new JPanel(); componentMap = new HashMap(); setupWindowDressing(exch, nickname, frame, panel); frame.pack();//www. j av a2 s . c om frame.show(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textInput.requestFocusInWindow(); ConnectionFactory cf = new ConnectionFactory(); cf.setHost(host); cf.setRequestedHeartbeat(0); conn = cf.newConnection(); ch = conn.createChannel(); ch.exchangeDeclare(exch, "fanout"); String queueName = ch.queueDeclare().getQueue(); ch.queueBind(queueName, exch, ""); ch.basicConsume(queueName, true, new DefaultConsumer(ch) { public void handleShutdownSignal(String consumerTag, ShutdownSignalException s) { if (s.getReason() instanceof java.io.EOFException) { JOptionPane.showMessageDialog(frame, "AMQP server disconnected.", "Connection closed", JOptionPane.ERROR_MESSAGE); } else { SwingUtil.complain("Connection closed", null, s); } System.exit(1); } public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String routingKey = envelope.getRoutingKey(); String contentType = properties.getContentType(); if (contentType.equals("text/plain")) { handleText(routingKey, new String(body)); return; } CamstreamComponent comp; if (!componentMap.containsKey(routingKey)) { comp = new CamstreamComponent(mixerSpec); addComponent(routingKey, comp); frame.pack(); } else { comp = (CamstreamComponent) componentMap.get(routingKey); } if (comp.handleDelivery(contentType, body)) { frame.pack(); } } }); }
From source file:net.orzo.queue.AmqpConnection.java
License:Apache License
@Override public void start() throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.conf.host); factory.setPort(this.conf.port); factory.setVirtualHost(this.conf.virtualHost); factory.setUsername(this.conf.user); factory.setPassword(this.conf.password); this.connection = factory.newConnection(); }
From source file:net.roboconf.messaging.internal.AbstractRabbitMqTest.java
License:Apache License
/** * Creates a channel to interact with a RabbitMQ server for tests. * @return a non-null channel/* ww w . j av a2 s . c o m*/ * @throws IOException if the creation failed */ protected Channel createTestChannel() throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(MESSAGE_SERVER_IP); Channel channel = factory.newConnection().createChannel(); return channel; }
From source file:net.roboconf.messaging.internal.client.MessageServerClientRabbitMq.java
License:Apache License
@Override public void openConnection(final IMessageProcessor messageProcessor) throws IOException { // Already connected? Do nothing if (this.connected) return;// w ww . j a v a2s .com // Initialize the connection ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.messageServerIp); this.connection = factory.newConnection(); this.channel = this.connection.createChannel(); this.connected = true; // 1 agent or 1 dm <=> 1 queue String exchangeName = getExchangeName(); // Exchange declaration is idem-potent this.channel.exchangeDeclare(exchangeName, TOPIC); // Queue declaration is idem-potent this.queueName = this.applicationName + "." + this.sourceName; this.channel.queueDeclare(this.queueName, true, false, true, null); // Start to listen to the queue final QueueingConsumer consumer = new QueueingConsumer(this.channel); this.consumerTag = this.channel.basicConsume(this.queueName, true, consumer); new Thread("Roboconf - Queue listener for " + this.queueName) { @Override public void run() { final Logger logger = Logger.getLogger(MessageServerClientRabbitMq.this.loggerName); logger.fine(getName() + " starts listening to new messages."); // We listen to messages until the consumer is cancelled for (;;) { try { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); Message message = SerializationUtils.deserializeObject(delivery.getBody()); StringBuilder sb = new StringBuilder(); sb.append(MessageServerClientRabbitMq.this.sourceName); sb.append(" received a message "); sb.append(message.getClass().getSimpleName()); sb.append(" on routing key '"); sb.append(delivery.getEnvelope().getRoutingKey()); sb.append("'."); // FIXME: should be logged in finer logger.info(sb.toString()); messageProcessor.processMessage(message); } catch (ShutdownSignalException e) { logger.finest(MessageServerClientRabbitMq.this.sourceName + ": the message server is shutting down."); break; } catch (ConsumerCancelledException e) { logger.fine(getName() + " stops listening to new messages."); break; } catch (InterruptedException e) { logger.finest(Utils.writeException(e)); break; } catch (ClassNotFoundException e) { logger.severe(MessageServerClientRabbitMq.this.sourceName + ": a message could not be deserialized. Class cast exception."); logger.finest(Utils.writeException(e)); } catch (IOException e) { logger.severe(MessageServerClientRabbitMq.this.sourceName + ": a message could not be deserialized. I/O exception."); logger.finest(Utils.writeException(e)); } catch (Exception e) { e.printStackTrace(); } } }; }.start(); }
From source file:net.roboconf.messaging.internal.client.MessageServerClientRabbitMq.java
License:Apache License
@Override public void cleanAllMessagingServerArtifacts() throws IOException { if (this.connected) throw new IOException("This instance is already connected to the messaging server."); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.messageServerIp); Connection connection = factory.newConnection(); Channel channel = this.connection.createChannel(); channel.exchangeDelete(getExchangeName(true)); channel.exchangeDelete(getExchangeName(false)); channel.close();/*from www .j a v a 2s . com*/ connection.close(); }
From source file:net.roboconf.messaging.internal.client.MessageServerClientRabbitMqTest.java
License:Apache License
/** * A method to check whether RabbitMQ is running or not. * <p>//from w w w. j a v a 2 s.c o m * If it is not running, tests in this class will be skipped. * </p> */ @Before public void checkRabbitMQIsRunning() throws Exception { Assume.assumeTrue(this.running); Connection connection = null; Channel channel = null; try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(MESSAGE_SERVER_IP); connection = factory.newConnection(); channel = connection.createChannel(); } catch (Exception e) { Logger logger = Logger.getLogger(getClass().getName()); logger.warning("Tests are skipped because RabbitMQ is not running."); logger.finest(Utils.writeException(e)); this.running = false; Assume.assumeNoException(e); } finally { if (channel != null) channel.close(); if (connection != null) connection.close(); } }
From source file:net.roboconf.messaging.internal.RabbitMqTestUtils.java
License:Apache License
/** * Creates a channel to interact with a RabbitMQ server for tests. * @return a non-null channel//ww w . ja v a 2 s. c o m * @throws IOException if the creation failed */ public static Channel createTestChannel() throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(MESSAGE_SERVER_IP); Channel channel = factory.newConnection().createChannel(); return channel; }
From source file:net.roboconf.messaging.internal.utils.RabbitMqUtils.java
License:Apache License
/** * Configures the connection factory with the right settings. * @param factory the connection factory * @param messageServerIp the message server IP (can contain a port) * @param messageServerUsername the user name for the message server * @param messageServerPassword the password for the message server * @throws IOException if something went wrong *//*from w w w . j a va2s.c o m*/ public static void configureFactory(ConnectionFactory factory, String messageServerIp, String messageServerUsername, String messageServerPassword) throws IOException { Map.Entry<String, Integer> entry = findUrlAndPort(messageServerIp); factory.setHost(entry.getKey()); if (entry.getValue() > 0) factory.setPort(entry.getValue()); factory.setUsername(messageServerUsername); factory.setPassword(messageServerPassword); }
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/* w ww. ja va2 s . co 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(); }