Apache ActiveMQ Artemis and SpringBoot using JMS

Fri, Apr 2, 2021 2-minute read

These are my personal notes on ActiveMQ Artemis and SpringBoot integration. Just experimenting with Artemis for the first time.

artemis-spring-boot-starter.png

  • required configuration created in SpringBoot app:
spring.artemis.mode=native
spring.artemis.host=localhost
spring.artemis.port=61616
spring.artemis.user=admin
spring.artemis.password=admin
spring.jms.pub-sub-domain=true #this parameter ensures JS mand MQQTT pub/sub compatibility

prices.mqtt.east=prices.mqtt.east
  • Consumer component created in Java. My demo is working with NodeRed flow, which sends a data to queue using MQQT (periodically every 3 seconds) and data is interpreted as byte array. To stay compatible with Java and NodeRed producers, let’s expect byte array is coming in.
@Component
public class ArtemisConsumer {

  @JmsListener(destination = "${prices.mqtt.east}")
  public void receive(byte[] msg){
    System.out.println("Recieved Message: " + new String(msg));
  }
}
  • Producer flow created in NodeRed. I’m using MQTT as natively supported in NodeRed, so it doesn’t require any further installation. It’s nice that Artemis messages are routed among protocols. There is still a one queue, and we can send/receive messages using several protocols. In our example, messages are sent using MQQT and JMS and consumed using JMS. artemis-spring-boot-starter.png

  • Let’s create a producer in Java. Its very simple component we can use in SpringBoot RestController later.

@Component
public class ArtemisProducer {
  @Autowired
  JmsTemplate jmsTemplate;

  @Value("${prices.mqtt.east}")
  String destinationQueue;

  public void send(String msg) {
    jmsTemplate.convertAndSend(destinationQueue, msg.getBytes());
  }

}
  • While our Java consumer is receiving messages from NodeRed already, we should close the ring and create a small @RestController that allows us to use producer we just created.
@RestController
@RequestMapping("/amq")
@AllArgsConstructor
public class ProducerController {

  private final ArtemisProducer artemisProducer;

  @GetMapping
  public String send(@RequestParam String m) {
    artemisProducer.send(m);

    return "Message sent: "+m;
  }
}

To send a message using Java producer let’s just call URL in very simple way: http://localhost:8080/amq?m=my_message . We should see message received on the console. We should actually see many messages because while coding, our NodeRed producer is sending a message every 3 seconds. What a great worker!