From the course: Spring: Messaging with JMS

Example code using Spring JMS converter - Spring Tutorial

From the course: Spring: Messaging with JMS

Start my 1-month free trial

Example code using Spring JMS converter

- [Instructor] We created some Book, Customer, and Order POJOs. Now we need to set up a JMS message converter. In order to create a JMS message converter, we need to do a little bit more setup work for our project. Let's create a separate configuration called a JMS config. We'll do that by Right Clicking on the config, go to New, Java Class. We'll type in JmsConfig. Inside of it we're going to copy from our Application.java. We'll copy from lines 40 through 58. We'll cut those out, come back to our JmsConfig, and we'll paste those inside. We'll annotate our JmsConfig with that configuration so that Spring knows to look at the beans in this as part of the configuration. We'll also annotate with EnableJms so that our JMS listeners will be picked up. Now, let's talk about the message converter. I mentioned that Spring makes available a default converter called a SimpleMessageConverter, which is used by default in transmission and conversion of JMS messages. Here we're going to specify a new bean which will cause Spring to use a different message converter methodology. And we'll just put this here at the top. We'll type @Bean. Public. MessageConverter. And we need to make sure that we use the jms.support.converter. And this is because Spring has two message supporter classes and they exist in different packages. If you choose the wrong one, things will not work properly. We'll call this a jacksonJms MessageConverter. And we'll instantiate a Mapping Jackson2MessageConverter. Again, Spring has two of these classes in different packages. You wanna use the one that's in the jms.support.converter. We'll call this a converter equals new MappingJackson2MessageConverter. MappingJackson2MessageConverter is designed for usage with JSON strings. So, now we need to type converter setTargetType. And we're going to set a target type of MessageType.TEXT. And this will use the underlying base JMS message type. So, we could use message type of bytes as well. We're going to use text here. And the last thing we'll type is converter.set TypeIdPropertyName. And we'll type in _type. Turn the converter. Now, the setTypeIdPropertyName, really doesn't matter what you set this to as long as the sender and the receiver both rely on the same message converter bean. I say this because if you decide to break this out into two separate projects, a warehouse API and a client API, make sure that whatever you set this type to is the same in both message converters. Otherwise your messages won't convert. Now, going back to the Application.java class. We're going to comment out a section inside the static void main method. And that's from line 34 through 37. So, we'll just type our comments here. And we're doing this because, as you'll find out in the next video, we'll be pushing our messages from the web frontend. And we don't really need this piece anymore. Now, going back to our JmsConfig class. We have one more thing to keep in mind here, and that is with the JmsTemplate bean we created earlier. The JmsTemplate bean we created is overwriting the default one provided by Spring which also means we have to do one of two things. First, if we leave this as is, then it means that we need to ensure the JmsTemplate variable is using our new message converter. And we could do that by typing template setMessageConverter and populating it with the JacksonMessageConverter we created. Doing so, however, means we don't have to set the message converter bean to Spring bean with the @Bean configuration. The @Bean causes the method it annotates to load an instance of that variable into the session memory at application startup. So, the application, through Spring API, calls it once with the @Bean, and then calling it again would be a waste of resources. The second way to handle this is to remove the JmsTemplate bean altogether. But then we would need to add an @Bean annotation on the ConnectionFactory to make it available because the internal JmsTemplate will need that ConnectionFactory set. Otherwise, Spring Boot will assume it is defaulting to its own internal ActiveMQ instance, and we really don't want that. Because Spring wires things together by looking first at the configuration class, beans, and then later the service component, et cetera. Class level annotations, we have to keep in mind what instances are already provided for us. For now, to keep this simple, we're gonna apply the @Bean annotation and we're going to remove the JmsTemplate. The last thing we need to do here is we need to create a new class and we're going to do this in our JMS service. We'll call this the BookStore Order Service. Inside of it we'll go ahead and apply the @Service annotation so Spring will pick it up. We will autowire a JmsTemplate to use the one Spring is specifying for us. And we'll create a private static final String variable for our queue destination. We'll call this our BOOK_QUEUE. And we'll make this be set to our book.order.queue. Then we'll create a new method. We'll call this public void send. And this will take a parameter of bookOrder. And inside of here we'll use our jmsTemplate.convertAndSend. We'll use our BOOK_QUEUE as our destination. And we'll send our bookOrder. As far as the warehouse receiver class, we'll add this as well in our next video. And then we'll run the app and introduce some frontend changes that I've made for you.

Contents