List of usage examples for com.rabbitmq.client ConnectionFactory newConnection
public Connection newConnection() throws IOException, TimeoutException
From source file:com.pcs.test.amqp.Consumer.java
License:Open Source License
public static void main(String[] args) throws IOException, TimeoutException, ShutdownSignalException, ConsumerCancelledException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("pcss-hdop04"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println("listen for messages"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String msg = new String(delivery.getBody(), "UTF-8"); System.out.println("Msg received " + msg); }//from w w w . ja va 2 s. c o m }
From source file:com.pcs.test.amqp.Publisher.java
License:Open Source License
public static void main(String[] args) throws IOException, TimeoutException { System.out.println("starting"); ConnectionFactory factory = new ConnectionFactory(); factory.setHost("pcss-hdop04"); factory.setAutomaticRecoveryEnabled(true); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "first message , hello world"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println("message sent"); channel.close();//from ww w. j a v a 2 s. co m connection.close(); }
From source file:com.project.finalproject.Recv.java
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(QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override/*from w w w . ja v a2 s .c o m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); } }; channel.basicConsume(QUEUE_NAME, true, consumer); }
From source file:com.project.finalproject.Send.java
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(QUEUE_NAME, false, false, false, null); try {//from w w w . j a va2s. c o m InputStream is = new FileInputStream("hr.json"); String jsontxt = IOUtils.toString(is); JSONObject jsonObject = new JSONObject(jsontxt); JSONArray stream = jsonObject.getJSONArray("stream"); for (int i = 0; i < stream.length(); i++) { String message = stream.getJSONObject(i).getString("value"); channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); TimeUnit.SECONDS.sleep(1); } } catch (FileNotFoundException fe) { fe.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } channel.close(); connection.close(); }
From source file:com.qt.core.util.MqConnectionUtil.java
License:Open Source License
private Connection createPooledConnection(MqConn conn) throws IOException { ConnectionFactory connectionFactory = new ConnectionFactory(); // connectionFactory.setHost(conn.getHost()); connectionFactory.setPort(conn.getPort()); connectionFactory.setUsername(conn.getAccount()); connectionFactory.setPassword(conn.getPassword()); Connection connection = connectionFactory.newConnection(); return connection; }
From source file:com.saasovation.common.port.adapter.messaging.rabbitmq.BrokerChannel.java
License:Apache License
/** * Constructs my default state.//from www. ja va 2 s .c o m * @param aConnectionSettings the ConnectionSettings * @param aName the String name of my implementor */ protected BrokerChannel(ConnectionSettings aConnectionSettings, String aName) { super(); ConnectionFactory factory = this.configureConnectionFactoryUsing(aConnectionSettings); this.setName(aName); try { this.setConnection(factory.newConnection()); this.setChannel(this.connection().createChannel()); } catch (IOException e) { throw new MessageException("Failed to create/open the queue.", e); } }
From source file:com.service.OperationFacadeREST.java
@POST @Override//from ww w .jav a2 s .com @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public void create(Operation entity) { if (!entity.getState().equals("waiting")) { super.create(entity); return; } if (entity.getOperationType())//venda { ArrayList<ClientStock> l = new ArrayList<>(getEntityManager() .find(Client.class, entity.getFkOwnerId().getClientId()).getClientStockCollection()); Boolean fail = false; for (int i = 0; i < l.size(); i++) { if (l.get(i).getStock().equals(entity.getFkStockId())) { if (l.get(i).getQuantity() < entity.getQuantity()) return; l.get(i).setQuantity(l.get(i).getQuantity() - entity.getQuantity()); getEntityManager().persist(l.get(i)); entity.getFkOwnerId().setClientStockCollection(l); entity.setCreationDate(Date.from(Instant.now())); super.create(entity); } } if (fail) return; } else { entity.setCreationDate(Date.from(Instant.now())); if (entity.getFkStockId().getQuantity() > entity.getQuantity()) { System.out.println("yes"); super.create(entity); } else { return; } } try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("hello", false, false, false, null); System.out.println(super.findAll().get(super.findAll().size() - 1).getOperationId() + "," + entity.getFkOwnerId().getName() + "," + entity.getFkStockId().getName() + "," + entity.getQuantity()); String message = super.findAll().get(super.findAll().size() - 1).getOperationId() + "," + entity.getFkOwnerId().getName() + "," + entity.getFkStockId().getName() + "," + entity.getQuantity(); channel.basicPublish("", "hello", null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); } catch (IOException ex) { Logger.getLogger(ClientFacadeREST.class.getName()).log(Level.SEVERE, null, ex); } catch (TimeoutException ex) { Logger.getLogger(ClientFacadeREST.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.simple.sftpfetch.publish.RabbitClient.java
License:Apache License
/** * Initialize the RabbitClient, establish a connection and declare the exchange * * @param factory the RabbitMQ ConnectionFactory * @param connectionInfo a bean containing the necessary connection information * * @throws NoSuchAlgorithmException/*from w w w .j a va2s. c o m*/ * @throws KeyManagementException * @throws URISyntaxException * @throws IOException */ public RabbitClient(ConnectionFactory factory, RabbitConnectionInfo connectionInfo) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException { factory.setHost(connectionInfo.getHostname()); factory.setPort(connectionInfo.getPort()); factory.setUsername(connectionInfo.getUsername()); factory.setPassword(connectionInfo.getPassword()); factory.setVirtualHost(connectionInfo.getVhost()); factory.setConnectionTimeout(connectionInfo.getTimeout()); Connection conn = factory.newConnection(); exchange = connectionInfo.getExchange(); channel = conn.createChannel(); channel.exchangeDeclare(exchange, EXCHANGE_TYPE, true); this.amqpProperties = new AMQP.BasicProperties.Builder().contentType(CONTENT_TYPE).deliveryMode(2).build(); }
From source file:com.sitewhere.connectors.rabbitmq.RabbitMqOutboundEventProcessor.java
License:Open Source License
@Override public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException { super.start(monitor); // Start multicaster if configured. if (multicaster != null) { startNestedComponent(multicaster, monitor, true); }//from w ww .j a v a2 s . com // Start route builder if configured. if (routeBuilder != null) { startNestedComponent(routeBuilder, monitor, true); } try { ConnectionFactory factory = new ConnectionFactory(); factory.setUri(getConnectionUri()); this.connection = factory.newConnection(); this.channel = connection.createChannel(); this.exchange = getTenantEngine().getTenant().getId() + DEFAULT_EXCHANGE_SUFFIX; channel.exchangeDeclare(exchange, "topic"); LOGGER.info("RabbitMQ outbound processor connected to: " + getConnectionUri()); } catch (Exception e) { throw new SiteWhereException("Unable to start RabbitMQ event processor.", e); } }
From source file:com.sitewhere.protobuf.test.ActiveMQTests.java
License:Open Source License
@Test public void doRabbitMQTest() throws Exception { String exchangeName = "sitewhere"; String queueName = "SITEWHERE.IN"; String routingKey = "sitewhere"; ConnectionFactory factory = new ConnectionFactory(); factory.setUri("amqp://localhost:5672/SITEWHERE.IN"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(exchangeName, "direct", true); channel.queueDeclare(queueName, true, false, false, null); channel.queueBind(queueName, exchangeName, routingKey); byte[] messageBodyBytes = generateEncodedMeasurementsMessage(); channel.basicPublish(exchangeName, routingKey, null, messageBodyBytes); channel.close();/* w w w. j ava 2 s . c o m*/ connection.close(); }