Пример #1
0
  public void sendMessage(String... varStrings) {
    String msg = varStrings[0];
    String exchangeName = varStrings[1];
    String qName = varStrings[2];
    String rabbitIp = varStrings[3];

    logger.debug("RabbitMQ Properties Start...");
    logger.debug("Message - " + msg);
    logger.debug("Exchange Name - " + exchangeName);
    logger.debug("QueueName - " + qName);
    logger.debug("IP Address - " + rabbitIp);
    logger.debug("RabbitMQ Properties End...");

    try {
      CachingConnectionFactory factory = new CachingConnectionFactory();
      factory.setAddresses(rabbitIp);
      org.springframework.amqp.rabbit.connection.Connection connection = factory.createConnection();
      Channel channel = connection.createChannel(false);
      channel.basicPublish(exchangeName, qName, null, msg.getBytes());
      channel.close();
      connection.close();
      logger.debug("Message Sent...");

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #2
0
  public void receiveMessage(String qName) {
    try {
      CachingConnectionFactory factory = new CachingConnectionFactory();
      factory.setAddresses("10.30.135.103:5672,10.30.135.101:5672");
      org.springframework.amqp.rabbit.connection.Connection connection = factory.createConnection();
      Channel channel = connection.createChannel(false);

      // channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
      // ring queueName = channel.queueDeclare().getQueue();
      // channel.queueBind(queueName, EXCHANGE_NAME, "");

      System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

      QueueingConsumer consumer = new QueueingConsumer(channel);
      channel.basicConsume(qName, true, consumer);

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

        System.out.println(" [x] Received '" + message + "'");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #3
0
  private void testConfig(
      final Map<String, String> diagnostics,
      final String configName,
      final CachingConnectionFactory factory) {
    final String connectKey = String.format(CAN_CONNECT, configName);
    final RabbitAdmin admin;
    try {
      factory.createConnection();
      admin = new RabbitAdmin(factory);
      diagnostics.put(connectKey, valueOf(true));
    } catch (Exception e) {
      diagnostics.put(connectKey, valueOf(false));
      return;
    }

    final boolean canAccessExchange = tryDeclaringExchange(admin);
    diagnostics.put(
        String.format(CAN_ACCESS_EXCHANGE, configName), String.valueOf(canAccessExchange));

    final boolean canDeclareQueue = tryUsingQueue(admin);
    diagnostics.put(String.format(CAN_USE_QUEUES, configName), String.valueOf(canDeclareQueue));
  }
 private Channel createBareChannel(
     ChannelCachingConnectionProxy connection, boolean transactional) {
   if (this.cacheMode == CacheMode.CHANNEL) {
     if (this.connection == null || !this.connection.isOpen()) {
       synchronized (this.connectionMonitor) {
         if (this.connection != null && !this.connection.isOpen()) {
           this.connection.notifyCloseIfNecessary();
           this.checkoutPermits.remove(this.connection);
         }
         if (this.connection == null || !this.connection.isOpen()) {
           this.connection = null;
           createConnection();
         }
       }
     }
     return doCreateBareChannel(this.connection, transactional);
   } else if (this.cacheMode == CacheMode.CONNECTION) {
     if (!connection.isOpen()) {
       synchronized (connectionMonitor) {
         this.openConnectionNonTransactionalChannels.get(connection).clear();
         this.openConnectionTransactionalChannels.get(connection).clear();
         connection.notifyCloseIfNecessary();
         ChannelCachingConnectionProxy newConnection =
             (ChannelCachingConnectionProxy) createConnection();
         /*
          * Applications already have a reference to the proxy, so we steal the new (or idle) connection's
          * target and remove the connection from the open list.
          */
         connection.target = newConnection.target;
         connection.closeNotified.set(false);
         this.openConnections.remove(newConnection);
       }
     }
     return doCreateBareChannel(connection, transactional);
   }
   return null;
 }