Ejemplo n.º 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();
    }
  }
 @Override
 public void onApplicationEvent(ContextRefreshedEvent event) {
   if (!this.lazyConnect
       && event.getApplicationContext().equals(getApplicationContext())
       && this.amqpTemplate instanceof RabbitTemplate) {
     ConnectionFactory connectionFactory =
         ((RabbitTemplate) this.amqpTemplate).getConnectionFactory();
     if (connectionFactory != null) {
       try {
         Connection connection = connectionFactory.createConnection();
         if (connection != null) {
           connection.close();
         }
       } catch (RuntimeException e) {
         logger.error("Failed to eagerly establish the connection.", e);
       }
     }
   }
 }