Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Properties:
- activemq.broker-url=(tcp://172.18.13.68:61616,tcp://172.18.13.69:61617)?ha=true&retryInterval=1000&reconnectAttempts=-1
- activemq.user=admin
- activemq.password=admin
- server.port=8082
- Code:
- Configuration class:
- @Value("${activemq.broker-url}")
- private String activeMqBrokerUri;
- @Value("${activemq.user}")
- private String username;
- @Value("${activemq.password}")
- private String password;
- @Bean
- public Connection connection() throws JMSException {
- ActiveMQConnectionFactory connFactory = new ActiveMQConnectionFactory(activeMqBrokerUri, username, password);
- Connection connection = connFactory.createConnection();
- connection.start();
- return connection;
- }
- Sender Class:
- public void sendDummyMessagesToTopic(String topicName) {
- Session session = null;
- MessageProducer msgProducer = null;
- try {
- session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- msgProducer = session.createProducer(session.createTopic(topicName));
- System.out.println("Iniciando envío...." + new Date());
- for (int i = 0; i < NUM_MESSAGES; i++) {
- String msg = "Message "+ i;
- TextMessage textMessage = session.createTextMessage(msg);
- try {
- msgProducer.send(textMessage);
- } catch (Exception e) {
- System.out.println("Fail sending message:" + i + ". Retrying...");
- i--;
- }
- }
- } catch (JMSException e) {
- System.out.println("Caught exception: " + e.getMessage());
- }
- try {
- if (msgProducer != null) {
- msgProducer.close();
- }
- if (session != null) {
- session.close();
- }
- } catch (Throwable ignore) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement