https://github.com/mageddo/spring-jms-example
https://github.com/mageddo/spring-jms-example
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mageddo/spring-jms-example
- Owner: mageddo
- Created: 2016-05-11T21:49:11.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2017-08-19T07:22:05.000Z (almost 9 years ago)
- Last Synced: 2025-03-27T23:30:21.487Z (about 1 year ago)
- Language: Java
- Size: 356 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.adoc
- License: LICENSE.code.txt
Awesome Lists containing this project
README
---
tags: [messaging, jms]
projects: [spring-framework]
---
:spring_boot_version: 1.3.5.RELEASE
:toc:
:icons: font
:source-highlighter: prettify
:project_id: gs-messaging-jms
This guide walks you through the process of publishing and subscribing to messages using a JMS broker.
== What you'll build
You'll build an application that uses Spring's `JmsTemplate` to post a single message and subscribes to it with a `@JmsListener` annotated method of a managed bean.
== What you'll need
:java_version: 1.8
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[]
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-gradle.adoc[]
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-maven.adoc[]
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-sts.adoc[]
[[initial]]
== Create a message receiver
Spring provides the means to publish messages to any link:/understanding/POJO[POJO].
`src/main/java/hello/Receiver.java`
[source,java]
----
include::complete/src/main/java/hello/Receiver.java[]
----
This is also known as a **message driven POJO**. As you can see in the code above, there is no need to implement any particular interface or for the method to have any particular name. Besides, the method may have a http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#jms-annotated-method-signature[very flexible signature]; in our simple example we will just extract the _payload_ of a `TextMessage`.
The `JmsListener` annotation defines the name of the `Destination` that this method should listen to and the reference to the `JmsListenerContainerFactory` to use to create the underlying message listener container. Strictly speaking that last attribute is not necessary unless you need to customize the way the container is built as Spring Boot registers a default factory if necessary.
The http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#jms-annotated[reference documentation] covers this in more detail.
== Send and receive JMS messages with Spring
Next, wire up a sender and a receiver.
`src/main/java/hello/Application.java`
[source,java]
----
include::complete/src/main/java/hello/Application.java[]
----
`@SpringBootApplication` is a convenience annotation that adds all of the following:
- `@Configuration` tags the class as a source of bean definitions for the application context.
- `@EnableAutoConfiguration` tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
- `@ComponentScan` tells Spring to look for other components, configurations, and services in the the `hello` package, allowing it to find the `Receiver`.
The `main()` method uses Spring Boot's `SpringApplication.run()` method to launch an application. Did you notice that there wasn't a single line of XML? No **web.xml** file either. This application is 100% pure Java and you didn't have to deal with configuring any plumbing or infrastructure.
`@EnableJms` triggers the discovery of methods annotated with `@JmsListener`, creating the message listener container under the covers.
For clarity, we have also defined a `myJmsContainerFactory` bean that is referenced in the `JmsListener` annotation of the receiver. This creates a `SimpleMessageListenerContainer`, an asynchronous message receiver that is fired up when the application context starts.
Spring provides a convenient template class called `JmsTemplate`. `JmsTemplate` makes it very simple to send messages to a JMS message queue. In the `main` runner method, after starting things up, you create a `MessageCreator` and use it from `jmsTemplate` to send a message.
Two beans that you don't see defined are `JmsTemplate` and `ActiveMQConnectionFactory`. These are created automatically by Spring Boot. In this case, the ActiveMQ broker runs embedded.
By default, Spring Boot creates a `JmsTemplate` configured to http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#jms-destinations[transmit to queues] by having **pubSubDomain** set to false. The `SimpleMessageListenerContainer` is also configured the same.
To override, set `spring.jms.isPubSubDomain=true` via Boot's property settings (either inside `application.properties` or by environment variable). Then make sure the receiving container
has the same setting.
NOTE: Spring's `JmsTemplate` can receive messages directly through its `receive` method, but that only works synchronously, meaning it will block. That's why Spring recommends that you use a listener container such as `SimpleMessageListenerContainer` with a cache-based connection factory, so you can consume messages asynchronously and with maximum connection efficiency.
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_subhead.adoc[]
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_with_both.adoc[]
When it runs, buried amidst all the logging, you should see these messages:
....
Sending a new message.
Received
....
== Summary
Congratulations! You've just developed a publisher and consumer of JMS-based messages.
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/footer.adoc[]