Skip to content

Lab M05P02

In this lab, you learn how to send and receive JMS messages using Spring artemis support.

Message producer (jmsproducer)

  1. An instance of the Apache Artemis broker shloud run on your machine. Broker should be configured to host exampleQueue and there shopuld be user user/uesr01* configured.

  2. First add Spring Boot Artemis started to project dependencies. Open the build.gradle script and add implementation 'org.springframework.boot:spring-boot-starter-artemis' into dependencies section. Then enable JMS autoconfiguration and add @EnableJms annotation on Application class.

  3. Review the application.properties file, where the Artemis connection and destination is configured.

  4. Now, implement the JmsProducer, which will send various messages to configured destination.
    The Spring Boot autoconfiguration configures the JmsTemplate for you, so you can use it to send messages. Inject the JmsTemplate insatnce into the JmsProducer Component (use @Component annotation)

  5. The Producer Rest Controller uses the JmsProducer to send messages to destination. The HTTP POST /send-message REST method produces a String message.
    Implement the sendStringMessage(String message) method which should send String JMS message using the JmsTemplate bean. Add also JMS message property named as type with value String. You can use Message Post Processor.

    Hint:

       public void sendStringMessage(String message) {
         LOG.info("Sending message {} ...", message);
         jmsTemplate.convertAndSend(queue, message, messagePostProcessor -> {
            messagePostProcessor.setStringProperty("type", "String");
            return messagePostProcessor;
         });
       }
    
  6. The HTTP POST /produce-book REST method of the Producer Rest Controller produces an Object message of type Book using JmsProducer. Implement the sendBook() method of the JmsProducer, which should send JMS message of type Obkect using the JmsTemplate bean. Add also JMS message property named as type with value Book. You can use Message Post Processor.

    Hint:

       public void sendBook(Book book) {
         jmsTemplate.convertAndSend(queue, book, messagePostProcessor -> {
            messagePostProcessor.setStringProperty("type",
                    "Book");
            return messagePostProcessor;
        });
       }
    
  7. Now, you can use Postman to produce String or Book message to exampleQueue destination.

  8. Test, if messages can be produced as part of Transaction.

Message consumer (jmsconsumer)

  1. Review the application.properties file, where the Artemis connection and destination is configured. Spring Boot Artemis starter is already configured in build.gradle.

  2. There is JmsConsumer Component already provided. Your task is to implement listeners to consume String and Book message types. You wil use a message selector.

  3. First, annotate the public void receiveMessage(String message) method with the @JmsListener annotation. You should provide the destination and selector annotation attributes. Destination name should be injected from cfg property. Selector is the spql expression (type='String').

    Hint:

        @JmsListener(destination = "${jms.queue.destination}", selector = "type = 'String'")
        @Transactional
        public void receiveMessage(String message) {
          LOG.info("Consumed String message: {}", message);
        }
    
  4. Then, annotate the public void receiveBook(Book book) method with the @JmsListener annotation. You should provide the destination and selector annotation attributes. Destination name should be injected from cfg property. Selector is the spql expression (type='Book').

    Hint:

      @JmsListener(destination = "${jms.queue.destination}",selector = "type = 'Book'")
      @Transactional
      public void receiveBook(Book book) {
        LOG.info("Consumed Book: {}", book.toString());
      }
    
  5. Now, start the Message Consumer and check the console for log messages.

  6. Test, if messages can be consumed as part of Transaction.