In the tutorial, we show how to build SpringBoot RestAPIs to produce/consume data from ActiveMQ.
Related posts:
– Spring Jms ActiveMq – How to send Java object messages to ActiveMQ server (specially with Bi-Directional relationship Java objects)
– How to use Spring JMS with ActiveMQ – JMS Consumer and JMS Producer | Spring Boot
– ActiveMq – Explicitly configure Spring ActiveMq ConnectionFactory with SpringBoot
– ActiveMq – How to work with Spring JMS ActiveMq Topic (Publisher-Subcribers pattern) using SpringBoot
Contents
Technologies
- Java 1.8
- Maven 3.3.9
- Spring Tool Suite – Version 3.9.4.RELEASE
- Spring Boot: 2.0.3.RELEASE
- ActiveMQ
Practice
We create a Spring JMS ActiveMQ with JMS Producer & JMS Consumer as below:
Then expose RestAPIs to POST/GET data to/from ActiveMQ:
@PostMapping(value="/api/customer")
@GetMapping(value="/api/customers")
Setup SpringBoot project
Use SpringToolSuite to create a SpringBoot project with below dependencies:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
ActiveMQ Connection Factory
ActiveMqConnectionFactoryConfig
->
package com.grokonez.activemq.config; import javax.jms.ConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.JmsListenerContainerFactory; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.support.converter.MappingJackson2MessageConverter; import org.springframework.jms.support.converter.MessageConverter; import org.springframework.jms.support.converter.MessageType; @Configuration public class ActiveMqConnectionFactoryConfig { @Value("${gkz.activemq.broker.url}") String brokerUrl; @Value("${gkz.activemq.borker.username}") String userName; @Value("${gkz.activemq.borker.password}") String password; /* * Initial ConnectionFactory */ @Bean public ConnectionFactory connectionFactory(){ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); connectionFactory.setBrokerURL(brokerUrl); connectionFactory.setUserName(userName); connectionFactory.setPassword(password); return connectionFactory; } @Bean // Serialize message content to json using TextMessage public MessageConverter jacksonJmsMessageConverter() { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setTargetType(MessageType.TEXT); converter.setTypeIdPropertyName("_type"); return converter; } /* * Used for Receiving Message */ @Bean public JmsListenerContainerFactory<?> jsaFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setMessageConverter(jacksonJmsMessageConverter()); configurer.configure(factory, connectionFactory); return factory; } /* * Used for Sending Messages. */ @Bean public JmsTemplate jmsTemplate(){ JmsTemplate template = new JmsTemplate(); template.setMessageConverter(jacksonJmsMessageConverter()); template.setConnectionFactory(connectionFactory()); return template; } } |
Add ActiveMQ configuration in application.properties
->
gkz.activemq.broker.url=tcp://localhost:61616 gkz.activemq.borker.username=admin gkz.activemq.borker.password=admin gkz.activemq.queue=gkz-queue |
Data Model
– Create Customer model ->
package com.grokonez.activemq.model; public class Customer { private Long id; private String firstname; private String lastname; private int age; public Customer(){ } public Customer(String firstname, String lastname, int age){ this.firstname = firstname; this.lastname = lastname; this.age = age; } public Customer(Long id, String firstname, String lastname, int age){ this.id = id; this.firstname = firstname; this.lastname = lastname; this.age = age; } // id public void setId(Long id){ this.id = id; } public Long getId(){ return this.id; } // firstname public void setFirstname(String firstname){ this.firstname = firstname; } public String getFirstname(){ return this.firstname; } // lastname public void setLastname(String lastname){ this.lastname = lastname; } public String getLastname(){ return this.lastname; } // age public void setAge(int age){ this.age = age; } public int getAge(){ return this.age; } } |
– Create MessageStorage
to storage Customer
list ->
package com.grokonez.activemq.model; import java.util.ArrayList; import java.util.List; public class MessageStorage { private List<Customer> customers = new ArrayList<>(); public void add(Customer customer) { customers.add(customer); } public void clear() { customers.clear(); } public List<Customer> getAll(){ return customers; } } |
Create a bean for MessageStorage
->
package com.grokonez.activemq.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.grokonez.activemq.model.MessageStorage; @Configuration public class BeanConfiguration { @Bean public MessageStorage customerStorage() { return new MessageStorage(); } } |
JMS Producer
JmsProducer
send messages to ActiveMQ:
package com.grokonez.activemq.jms.producer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Component; import com.grokonez.activemq.model.Customer; @Component public class JmsProducer { @Autowired JmsTemplate jmsTemplate; @Value("${gkz.activemq.queue}") String queue; public void send(Customer customer){ jmsTemplate.convertAndSend(queue, customer); } } |
JMS Consumer
JmsConsumer
recieves messages from ActiveMQ ->
package com.grokonez.activemq.jms.consumer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import com.grokonez.activemq.model.Customer; import com.grokonez.activemq.model.MessageStorage; @Component public class JmsConsumer { @Autowired private MessageStorage customerStorage; @JmsListener(destination = "${gkz.activemq.queue}", containerFactory="jsaFactory") public void receive(Customer customer){ System.out.println("Recieved Message: " + customer); customerStorage.add(customer); } } |
Rest APIs
RestAPIs
->
package com.grokonez.activemq.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.grokonez.activemq.jms.producer.JmsProducer; import com.grokonez.activemq.model.Customer; import com.grokonez.activemq.model.MessageStorage; @RestController public class RestAPIs { @Autowired JmsProducer jmsProducer; @Autowired private MessageStorage customerStorage; @PostMapping(value="/api/customer") public Customer postCustomer(@RequestBody Customer customer){ jmsProducer.send(customer); return customer; } @GetMapping(value="/api/customers") public List<Customer> getAll(){ List<Customer> customers = customerStorage.getAll(); return customers; } @DeleteMapping(value="/api/customers/clear") public String clearCustomerStorage() { customerStorage.clear(); return "Clear All CustomerStorage!"; } } |
Run & Check Results
– Build & Run SpringBoot project with commandlines {mvn clean install
, mvn spring-boot:run
}.
-> Post Messages to ActiveMQ:
-> Get Messages:
-> ActiveMQ state:
How can run ActiveMQ state?
please tell what if i need to send and receive data in xml.
will the same messageconverter workhs or we need to write another one