List of usage examples for com.rabbitmq.client ConnectionFactory ConnectionFactory
ConnectionFactory
From source file:hu.sztaki.stratosphere.workshop.RMQ.BasicRMQSender.java
License:Apache License
public static void main(String[] argv) throws java.io.IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String message;/*from w ww . j ava2 s. co m*/ while (true) { System.out.println("Enter next message (q to quit):"); message = br.readLine(); if (message.equals("q")) { channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); break; } channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); } channel.close(); connection.close(); }
From source file:hu.sztaki.stratosphere.workshop.RMQ.RMQALSSender.java
License:Apache License
public static void main(String[] argv) throws java.io.IOException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); Random rnd = new Random(); for (int i = 0; i < 1000; i++) { Thread.sleep(10);//from w w w.jav a 2s . com channel.basicPublish("", QUEUE_NAME, null, Integer.toString(rnd.nextInt(250)).getBytes()); } channel.close(); connection.close(); }
From source file:in.cs654.chariot.ashva.AshvaServer.java
License:Open Source License
public static void main(String[] args) { Connection connection = null; Channel channel;/* w w w . j a va 2s. c o m*/ try { final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST_IP_ADDR); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); channel.basicQos(1); final QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(RPC_QUEUE_NAME, false, consumer); LOGGER.info("Ashva Server started. Waiting for requests..."); AshvaHelper.joinOrStartChariotPool(); AshvaHelper.startHeartbeat(); while (true) { final QueueingConsumer.Delivery delivery = consumer.nextDelivery(); BasicResponse response = new BasicResponse(); BasicRequest request = new BasicRequest(); final BasicProperties props = delivery.getProperties(); final BasicProperties replyProps = new BasicProperties.Builder() .correlationId(props.getCorrelationId()).build(); try { final DatumReader<BasicRequest> avroReader = new SpecificDatumReader<BasicRequest>( BasicRequest.class); decoder = DecoderFactory.get().binaryDecoder(delivery.getBody(), decoder); request = avroReader.read(request, decoder); response = AshvaProcessor.process(request); } catch (Exception e) { e.printStackTrace(); LOGGER.severe("Error in handling request: " + e.getMessage()); response = ResponseFactory.getErrorResponse(request); } finally { baos.reset(); final DatumWriter<BasicResponse> avroWriter = new SpecificDatumWriter<BasicResponse>( BasicResponse.class); encoder = EncoderFactory.get().binaryEncoder(baos, encoder); avroWriter.write(response, encoder); encoder.flush(); LOGGER.info("Responding to request id " + request.getRequestId() + " " + response.getStatus()); channel.basicPublish("", props.getReplyTo(), replyProps, baos.toByteArray()); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } } catch (Exception e) { e.printStackTrace(); LOGGER.severe("Error in RPC server: " + e.getMessage()); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
From source file:in.cs654.chariot.prashti.PrashtiServer.java
License:Open Source License
public static void main(String[] args) { Connection connection = null; Channel channel;//from w ww .j a v a2s . c om try { final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST_IP_ADDR); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); channel.basicQos(1); final QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(RPC_QUEUE_NAME, false, consumer); LOGGER.info("Prashti Server started. Waiting for requests..."); final List<Prashti> prashtiList = D2Client.getOnlinePrashtiServers(); final String ipAddr = CommonUtils.getIPAddress(); prashtiList.add(new Prashti(ipAddr)); LOGGER.info("Notifying D2 to set Prashti Server IP Address"); D2Client.setPrashtiServers(prashtiList); while (true) { final QueueingConsumer.Delivery delivery = consumer.nextDelivery(); BasicResponse response = new BasicResponse(); BasicRequest request = new BasicRequest(); final BasicProperties props = delivery.getProperties(); final BasicProperties replyProps = new BasicProperties.Builder() .correlationId(props.getCorrelationId()).build(); try { final DatumReader<BasicRequest> avroReader = new SpecificDatumReader<BasicRequest>( BasicRequest.class); decoder = DecoderFactory.get().binaryDecoder(delivery.getBody(), decoder); request = avroReader.read(request, decoder); response = RequestProcessor.process(request); } catch (Exception e) { e.printStackTrace(); LOGGER.severe("Error in handling request: " + e.getMessage()); response = ResponseFactory.getErrorResponse(request); } finally { baos.reset(); final DatumWriter<BasicResponse> avroWriter = new SpecificDatumWriter<BasicResponse>( BasicResponse.class); encoder = EncoderFactory.get().binaryEncoder(baos, encoder); avroWriter.write(response, encoder); encoder.flush(); LOGGER.info("Responding to request id " + request.getRequestId() + " " + response.getStatus()); channel.basicPublish("", props.getReplyTo(), replyProps, baos.toByteArray()); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } } catch (Exception e) { e.printStackTrace(); LOGGER.severe("Error in RPC server: " + e.getMessage()); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
From source file:in.cs654.chariot.turagraksa.ZooKeeperServer.java
License:Open Source License
public static void main(String[] args) { Connection connection = null; Channel channel;/*from ww w .j a v a2s .c o m*/ try { final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST_IP_ADDR); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); channel.basicQos(1); final QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(RPC_QUEUE_NAME, false, consumer); LOGGER.info("ZooKeeper Server started. Waiting for requests..."); ZooKeeper.startPingEcho(); while (true) { final QueueingConsumer.Delivery delivery = consumer.nextDelivery(); BasicResponse response = new BasicResponse(); BasicRequest request = new BasicRequest(); final AMQP.BasicProperties props = delivery.getProperties(); final AMQP.BasicProperties replyProps = new AMQP.BasicProperties.Builder() .correlationId(props.getCorrelationId()).build(); try { final DatumReader<BasicRequest> avroReader = new SpecificDatumReader<BasicRequest>( BasicRequest.class); decoder = DecoderFactory.get().binaryDecoder(delivery.getBody(), decoder); request = avroReader.read(request, decoder); response = ZooKeeperProcessor.process(request); } catch (Exception e) { e.printStackTrace(); LOGGER.severe("Error in handling request: " + e.getMessage()); response = ResponseFactory.getErrorResponse(request); } finally { baos.reset(); final DatumWriter<BasicResponse> avroWriter = new SpecificDatumWriter<BasicResponse>( BasicResponse.class); encoder = EncoderFactory.get().binaryEncoder(baos, encoder); avroWriter.write(response, encoder); encoder.flush(); channel.basicPublish("", props.getReplyTo(), replyProps, baos.toByteArray()); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } } catch (Exception e) { e.printStackTrace(); LOGGER.severe("Error in RPC server: " + e.getMessage()); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
From source file:in.cs654.chariot.utils.AshvaClient.java
License:Open Source License
/** * Constructor to build AshvaClient/*from w ww . j a v a2s .c o m*/ * @param ipAddr IP address of the ashva server */ public AshvaClient(String ipAddr) { final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(ipAddr); try { connection = factory.newConnection(); channel = connection.createChannel(); replyQueueName = channel.queueDeclare().getQueue(); consumer = new QueueingConsumer(channel); channel.basicConsume(replyQueueName, true, consumer); baos = new ByteArrayOutputStream(); } catch (Exception ignore) { } }
From source file:info.pancancer.arch3.utils.Utilities.java
License:Open Source License
/** * Clears database state and known queues for testing. * * @param settings/* ww w . j a v a 2 s . co m*/ * @throws IOException * @throws java.util.concurrent.TimeoutException */ public static void clearState(HierarchicalINIConfiguration settings) throws IOException, TimeoutException { File configFile = FileUtils.getFile("src", "test", "resources", "config"); HierarchicalINIConfiguration parseConfig = Utilities.parseConfig(configFile.getAbsolutePath()); PostgreSQL postgres = new PostgreSQL(parseConfig); // clean up the database postgres.clearDatabase(); String server = settings.getString(Constants.RABBIT_HOST); String user = settings.getString(Constants.RABBIT_USERNAME); String pass = settings.getString(Constants.RABBIT_PASSWORD); Channel channel; ConnectionFactory factory = new ConnectionFactory(); factory.setHost(server); factory.setUsername(user); factory.setPassword(pass); factory.setAutomaticRecoveryEnabled(true); Connection connection = factory.newConnection(); channel = connection.createChannel(); channel.basicQos(1); channel.confirmSelect(); String prefix = settings.getString(Constants.RABBIT_QUEUE_NAME); String[] queues = { prefix + "_jobs", prefix + "_orders", prefix + "_vms", prefix + "_for_CleanupJobs", prefix + "_for_CleanupVMs" }; for (String queue : queues) { try { channel.queueDelete(queue); } catch (IOException e) { Log.info("Could not delete " + queue); } } }
From source file:info.pancancer.arch3.utils.Utilities.java
License:Open Source License
public static Channel setupQueue(HierarchicalINIConfiguration settings, String queue) throws IOException, TimeoutException { String server = settings.getString(Constants.RABBIT_HOST); String user = settings.getString(Constants.RABBIT_USERNAME); String pass = settings.getString(Constants.RABBIT_PASSWORD); Channel channel = null;/* w ww . jav a2 s.c o m*/ try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(server); factory.setUsername(user); factory.setPassword(pass); factory.setAutomaticRecoveryEnabled(true); Connection connection = factory.newConnection(); channel = connection.createChannel(); channel.basicQos(1); channel.queueDeclare(queue, true, false, false, null); channel.confirmSelect(); // channel.queueDeclarePassive(queue); } catch (IOException | TimeoutException ex) { // Logger.getLogger(Master.class.getName()).log(Level.SEVERE, null, ex); LOG.error("Error setting up queue connections to queue:" + queue + " on host: " + server + "; error is: " + ex.getMessage(), ex); } return channel; }
From source file:info.pancancer.arch3.utils.Utilities.java
License:Open Source License
public static Channel setupExchange(HierarchicalINIConfiguration settings, String queue) throws IOException, TimeoutException { String server = settings.getString(Constants.RABBIT_HOST); String user = settings.getString(Constants.RABBIT_USERNAME); String pass = settings.getString(Constants.RABBIT_PASSWORD); Channel channel = null;/*from w w w. j a va 2 s.c o m*/ try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(server); factory.setUsername(user); factory.setPassword(pass); factory.setAutomaticRecoveryEnabled(true); Connection connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(queue, "fanout", true, false, null); channel.confirmSelect(); } catch (IOException | TimeoutException ex) { // Logger.getLogger(Master.class.getName()).log(Level.SEVERE, null, ex); LOG.error("Error setting up queue connections: " + ex.getMessage(), ex); throw ex; } return channel; }
From source file:io.bigwig.sasl.PublicKeyITest.java
License:Mozilla Public License
@Test public void testDefaultUser() throws Exception { // Read in a private key from the file system and pass it to the SASL configuration InputStream is = getClass().getClassLoader().getResourceAsStream("ecdsa.pem"); PublicKeyConfig saslConfig = new PublicKeyConfig(new InputStreamReader(is)); ConnectionFactory factory = new ConnectionFactory(); factory.setSaslConfig(saslConfig);//from ww w .j av a2s . c o m // Override any other non-default parameters for connecting to Bigwig, for example: // factory.setUsername("gljJvcFZwoMW9"); // factory.setPort(10160); // factory.setVirtualHost("B6F90F1uhpNv"); Connection connection = factory.newConnection(); assertTrue("Could not open connection to Bigwig using public key authentication", connection.isOpen()); connection.close(); }