Overview

Chapter 7. Creating Java applications with ActiveMQ

This chapter shifts ActiveMQ from being just a broker to being a practical part of Java applications. It shows that you can either embed a broker directly in your code or configure it through XML, depending on how much control and flexibility your application needs. The overall message is that ActiveMQ can be integrated in a variety of ways, from simple standalone Java classes to more framework-driven solutions.

One major theme is embedding ActiveMQ with plain Java or with Spring-based configuration. The chapter explains how to create and start a broker with the broker service API, how to reuse existing XML configurations through a broker factory, and how to use Spring XML, factory beans, or XBean to simplify setup. It also emphasizes that configuration style is a design choice: some approaches are more compact or maintainable, but none is universally best for every application.

The chapter also introduces common messaging patterns and client development techniques. It walks through a request/reply design in JMS, where producers send requests, workers process them, and replies are matched using correlation IDs and reply destinations; this pattern is highlighted as highly scalable because workers can be added independently. Finally, it shows how Spring JMS reduces boilerplate for connections, destinations, consumers, and producers, making it easier to build JMS clients and reuse business logic while letting Spring handle much of the messaging infrastructure.

Local and remote applications exchange messages with an embedded broker.
Listing 7.2. Configure ActiveMQ with security plug-ins using Java
Listing 7.8. ActiveMQ XML configuration for projects using Spring syntax
Listing 7.10. The XBeanBroker class
Listing 7.12. ActiveMQ XML configuration using Spring 2.x (and newer)
Overview of a request/reply implementation using JMS
Listing 7.14. Create a broker, a consumer, and a producer for the request/reply example
Listing 7.15. The message listener for the request/reply example
Listing 7.18. Implementation of logic for request/reply client
Listing 7.24. JMS publisher implementation in Spring
Listing 7.26. The main method for the Spring example

FAQ

What is the main goal of Chapter 7 in ActiveMQ in Action?The chapter focuses on how to create Java applications with ActiveMQ, including embedding a broker, building request/reply systems, and using Spring to simplify JMS client development.
Why would you embed ActiveMQ inside a Java application?Embedding ActiveMQ lets your application run a broker locally as part of the same Java process, making it easy for internal components to communicate over the VM protocol while still allowing remote clients to connect over the network.
What is the BrokerService class used for?The BrokerService class is the main Java API for configuring and managing an embedded ActiveMQ broker, including setting broker properties, adding plugins, adding transport connectors, and starting or stopping the broker.
What is the BrokerFactory class used for?The BrokerFactory class creates a BrokerService from a broker URI, allowing you to load broker configuration from XML or other supported URI-based configuration schemes such as xbean:.
What does the xbean: URI scheme do in ActiveMQ?The xbean: URI tells BrokerFactory to use the XBean broker factory and load the broker configuration from the specified XML file, either on the classpath or the file system.
What is the difference between pure Spring XML and ActiveMQ’s custom XML namespace?Pure Spring XML uses standard Spring <bean> definitions, while the custom XML namespace uses ActiveMQ-specific schema elements such as <amq:broker> for a more compact configuration style.
What is the BrokerFactoryBean used for in Spring applications?The BrokerFactoryBean lets Spring manage the creation and startup of an ActiveMQ broker using a configuration file, reducing the amount of custom setup code needed in a Spring application.
How does the request/reply pattern work with JMS and ActiveMQ?A client sends a request message with a correlation ID and reply destination, a worker processes it and sends a response to the reply destination, and the client matches the reply to the original request using the correlation ID.
What role does Spring JMS play in ActiveMQ client development?Spring JMS reduces boilerplate by providing helpers such as DefaultMessageListenerContainer for consumers and JmsTemplate for producers, making JMS applications easier to configure and maintain.
Why is a pooled connection factory recommended with JmsTemplate?JmsTemplate creates and destroys JMS resources for each send operation, so using a pooled connection factory improves performance when not running inside a Java EE container that already provides connection pooling.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • ActiveMQ in Action ActiveMQ in Action ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • ActiveMQ in Action ActiveMQ in Action ebook for free