M
M
MaxLich2018-10-03 16:37:27
Java
MaxLich, 2018-10-03 16:37:27

How and when are connections created by an object of the CachingConnectionFactory class?

Hello. I use activiti 6, and service tasks in the diagram. Almost every service task has the same class that sends some message. Judging by the activity documentation, an instance of this class is created one for each service task. That is, how many service tasks, so many instances of this class will be (but no more). In the class constructor, I create an instance of the org.springframework.amqp.rabbit.connection.CachingConnectionFactory class:

JdbcTemplateNsiDao jdbcTemplateNsiDao = new JdbcTemplateNsiDao();
            jdbcTemplateRlDao = new JdbcTemplateRlDao();

            String hostName = jdbcTemplateNsiDao.getHostName();
            Long port = jdbcTemplateNsiDao.getPort();

            logger.info(String.format("Create the ConnectionFactory for the RabbitMQ with host name = %s and port = %d", hostName, port));
            CachingConnectionFactory connectionFactory =
                    new CachingConnectionFactory(hostName, port.intValue());

            sendService = new SendServiceImpl(new RabbitTemplate(connectionFactory));
            ((SendToETPServiceImpl) sendToETPService).setQueueNameForStatusMovement(jdbcTemplateNsiDao.getQueueNameForStatusMovement());
        } catch (NamingException | IOException e) {
          logger.error("Error while creating JdbcTemplate Dao for Activiti ", e);
        }
    }

I send the message already in the SendServiceImpl class, using the following code:
rabbitTemplate.convertAndSend(queueNameForStatusMovement, message);

Question: how many connections to RabbitMQ will be created and when are they created? One per factory, or will a connection be created each time it is sent? Now there is a feeling that more than one connection per factory is being created.
And another question: how are all open connections closed? Need to explicitly call destroy()?
ZY Unfortunately, I cannot create RabbitTemplate and CachingConnectionFactory objects as spring container beans. As I understand it, if they are created as beans, then there are no problems with a large number of connections, and the spring itself closes the connections when the context is closed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MaxLich, 2018-10-05
@MaxLich

The problem has become not very relevant, since it was possible to use the spring beans from the activity.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question