Chapter 7: Chapter 7. Creating Java applications with ActiveMQ
This introduction presents an embedded ActiveMQ broker as a way for Java applications to serve both local and remote messaging clients.
After this chapter you will be able to
- Embedded broker client access. Explain how ActiveMQ can be embedded in a Java application and serve both local and remote clients.
- Java BrokerService configuration. Describe how the example configures and starts a broker using BrokerService instead of XML.
- Broker initialization ordering. Explain the required order for adding plug-ins and connectors and starting the broker, including the consequences of getting it wrong.
- BrokerFactory and XBean configuration. Explain how BrokerFactory uses a configuration URI to create a BrokerService and how the xbean: scheme locates an XML configuration.
- URI-only broker configuration. Describe how the broker: URI scheme can configure a simple broker without an XML configuration file, using the section's example.
- Spring configuration and embedding. Explain how ActiveMQ’s default configuration relates to embedding a broker in a Spring-enabled application.
- Embedding method selection. Explain why there is no universally best Spring embedding method and what considerations guide the choice.
- Pure Spring broker configuration. Describe how pure Spring XML configures an ActiveMQ broker and how it relates to direct Java API configuration.
- Spring configuration startup. Describe how the example starts the broker from its pure Spring configuration.
- BrokerFactoryBean purpose and benefits. Explain how BrokerFactoryBean uses Spring’s factory bean approach for ActiveMQ and why it is useful.
- Broker configuration and startup. Describe how the config and start properties configure and start a broker through BrokerFactoryBean, including the manual-start alternative.
- XBean configuration syntax. Explain how ActiveMQ uses Spring and XBean for configuration and why XBean’s XML syntax is useful.
- XBean application startup. Trace the example application’s setup and message publishing, identifying the source of its application context class.
- ActiveMQ namespace resolution. Explain how the ActiveMQ namespace prefix, URI, and schemaLocation relate when a Spring configuration uses an element such as <amq:broker>.
- Asynchronous request reply architecture. Explain how a JMS request/reply system uses a broker and asynchronous messaging to exchange a request and a reply.
- Reply routing and correlation. Trace how JMSReplyTo and JMSCorrelationID let a worker return a reply and the client associate it with the original request.
- Distributed worker scaling. Explain how adding workers, including on separate hosts, helps a messaging-based request/reply system handle greater load.
- Server and worker startup. Describe how the example starts the embedded broker and prepares the worker to receive requests.
- Worker reply handling. Trace how the worker listener responds to a request, including its reply destination and correlation ID.
- Request and reply routing. Trace how the client prepares and sends a request and where it listens for the reply.
- Running the request reply example. Describe how to start the request/reply example and what its client produces.
- Scaling with asynchronous messaging. Explain how multiple brokers and workers support the large-scale request/reply scenario described in this section.
- ActiveMQ connection and destination configuration. Describe how ActiveMQ classes support configuring JMS connections and destinations for an application.
- Spring JMS client helpers. Describe the roles of Spring's helper classes in configuring JMS consumers and producers.
- ActiveMQ connection factory role. Explain how an ActiveMQConnectionFactory relates to connecting a JMS application to the broker and how it can be used with Spring.
- Pooled connection factory configuration. Describe what PooledConnectionFactory pools, why an application might use it, and how its connectionFactory property is configured in the example.
- Predefined JMS destinations. Explain how JMS destinations can be predefined in activemq.xml and why doing so can be useful.
- Message listener container role. Explain how a Spring message listener container separates message-receiving infrastructure from listener business logic.
- Portfolio consumer container setup. Describe the basic container configuration in the portfolio example and what happens when the containers start.
- Spring message creation and sending. Explain how a MessageCreator works with JmsTemplate to send a JMS message, using the stock publisher as an example.
- Connection pooling for sends. Explain why the section recommends a pooled connection factory when using JmsTemplate outside a Java EE container.
- Spring Example Startup. Describe how the example starts the broker and JMS clients and what appears as it runs.
What it builds on
- Configuring the simple authentication plug-in (chapter 6) · The simple authentication plug-in configuration used as the example
- Introduction to enterprise messaging (chapter 2) · The basic idea of enterprise messaging.
- JMS message internals (chapter 2) · JMS message properties.
- A simple JMS application (chapter 2) · Basic JMS application objects
- Authentication (chapter 6) · The concept of broker authentication for clients.
- Use case one: the stock portfolio example (chapter 3) · The stock portfolio publisher supplies the example adapted for Spring.
Section by section
Introduction
This introduction presents an embedded ActiveMQ broker as a way for Java applications to serve both local and remote messaging clients.
- Embedded broker client access: Explain how ActiveMQ can be embedded in a Java application and serve both local and remote clients.
Why it matters: An embedded broker can support messaging inside an application without limiting access to that application alone.
Key terms: embedded broker, VM protocol, remote applications
7.1 Embedding ActiveMQ using Java
This section introduces Java-based approaches to embedding and configuring an ActiveMQ broker.
7.1.1 Embedding ActiveMQ using the BrokerService
This section demonstrates configuring and starting an embedded ActiveMQ broker in Java with BrokerService, including important initialization order and when this approach is useful.
- Java BrokerService configuration: Describe how the example configures and starts a broker using BrokerService instead of XML.
Why it matters: This shows how to initialize a broker directly within a Java application. - Broker initialization ordering: Explain the required order for adding plug-ins and connectors and starting the broker, including the consequences of getting it wrong.
Why it matters: The order determines whether the broker's plug-ins and connectors initialize properly. - Broker configuration approach choice: Distinguish when direct Java configuration with BrokerService is useful from the need to reuse standalone broker configuration files.
Why it matters: The choice depends on whether the application needs configuration outside its Java code.
Key terms: BrokerService, brokerName, dataDirectory, SimpleAuthenticationPlugin, setPlugins(), addConnector(), plug-ins, connectors, broker start, BrokerFactory
7.1.2 Embedding ActiveMQ using the BrokerFactory
This section explains how to create an embedded ActiveMQ broker with BrokerFactory using XML-based or URI-only configuration.
- BrokerFactory and XBean configuration: Explain how BrokerFactory uses a configuration URI to create a BrokerService and how the xbean: scheme locates an XML configuration.
Why it matters: This lets you create an embedded broker from an existing ActiveMQ XML configuration. - URI-only broker configuration: Describe how the broker: URI scheme can configure a simple broker without an XML configuration file, using the section's example.
Why it matters: A broker URI can provide a compact alternative for simple broker configuration.
Key terms: BrokerFactory, BrokerService, BrokerFactory.createBroker(), XBeanBrokerFactory, xbean: URI scheme, broker: URI scheme, network connectors, transport connectors, persistence, JMX
7.2 Embedding ActiveMQ using Spring
This section introduces ActiveMQ’s compatibility with Spring and the considerations for choosing an embedding method.
- Spring configuration and embedding: Explain how ActiveMQ’s default configuration relates to embedding a broker in a Spring-enabled application.
Why it matters: This connection explains why Spring applications are a natural place to embed an ActiveMQ broker. - Embedding method selection: Explain why there is no universally best Spring embedding method and what considerations guide the choice.
Why it matters: Choosing an embedding approach requires weighing its fit for the application and the team that must maintain it.
Key terms: Spring XML configuration file, embed, ActiveMQ broker, application and system architecture, skill set, maintain the solution
7.2.1 Pure Spring XML
This section explains how to configure and start an embedded ActiveMQ broker using pure Spring XML.
- Pure Spring broker configuration: Describe how pure Spring XML configures an ActiveMQ broker and how it relates to direct Java API configuration.
Why it matters: This gives you another way to configure an embedded broker without writing the configuration directly in Java. - Spring configuration startup: Describe how the example starts the broker from its pure Spring configuration.
Why it matters: The configuration must be loaded to start the embedded broker.
Key terms: pure Spring configuration, BrokerService, Spring container, ActiveMQ Java API, SpringConfig, ClassPathXmlApplicationContext, main method
7.2.2 Using the BrokerFactoryBean
This section explains how Spring’s BrokerFactoryBean simplifies configuring and starting an ActiveMQ broker.
- BrokerFactoryBean purpose and benefits: Explain how BrokerFactoryBean uses Spring’s factory bean approach for ActiveMQ and why it is useful.
Why it matters: This approach lets you integrate a broker into Spring without maintaining your own factory class or extensive configuration XML. - Broker configuration and startup: Describe how the config and start properties configure and start a broker through BrokerFactoryBean, including the manual-start alternative.
Why it matters: These properties determine where the broker gets its configuration and whether it starts automatically.
Key terms: Spring factory beans, BrokerFactoryBean, Spring container, config, start, ActiveMQ XML configuration file
7.2.3 Using Apache XBean with Spring
This section explains ActiveMQ’s use of XBean-backed Spring configuration and illustrates starting a broker from a Java application.
- XBean configuration syntax: Explain how ActiveMQ uses Spring and XBean for configuration and why XBean’s XML syntax is useful.
Why it matters: This explains the basis of the configuration files used throughout the book. - XBean application startup: Trace the example application’s setup and message publishing, identifying the source of its application context class.
Why it matters: This shows how a small Java application delegates broker setup to its XML configuration and framework.
Key terms: activemq.xml, Spring, Apache XBean, custom XML schema, XBeanBroker, activemq.base, Spring application context, publisher
7.2.4 Using a custom XML namespace with Spring
This section explains how a Spring configuration uses ActiveMQ’s custom XML namespace to define broker-related beans.
- ActiveMQ namespace resolution: Explain how the ActiveMQ namespace prefix, URI, and schemaLocation relate when a Spring configuration uses an element such as <amq:broker>.
Why it matters: This relationship explains how a Spring XML file identifies ActiveMQ-specific configuration elements. - Custom broker configuration syntax: Describe what ActiveMQ’s custom Spring schema lets a developer configure and identify the example’s security plug-in.
Why it matters: The custom syntax provides a way to express broker configuration directly in a Spring configuration file.
Key terms: XML namespace, amq, URI, schemaLocation, XSD, <amq:broker>, custom XML schema, Spring configuration file, broker-related beans, simple authentication plug-in
7.3 Implementing request/reply with JMS
This section explains the JMS request/reply pattern, how replies are routed and matched to requests, and how workers can be scaled.
- Asynchronous request reply architecture: Explain how a JMS request/reply system uses a broker and asynchronous messaging to exchange a request and a reply.
Why it matters: This establishes the roles and communication style behind the request/reply example. - Reply routing and correlation: Trace how JMSReplyTo and JMSCorrelationID let a worker return a reply and the client associate it with the original request.
Why it matters: Routing and correlation ensure that a reply reaches the client and can be matched to the right request. - Distributed worker scaling: Explain how adding workers, including on separate hosts, helps a messaging-based request/reply system handle greater load.
Why it matters: This shows why asynchronous request/reply can scale beyond a single worker.
Key terms: request/reply, broker, producer, consumer, asynchronous, JMSReplyTo, JMSCorrelationID, reply destination, correlation ID, temporary JMS destination
7.3.1 Implementing the server and the worker
This section implements and starts an embedded broker and a worker that responds to JMS requests.
- Server and worker startup: Describe how the example starts the embedded broker and prepares the worker to receive requests.
Why it matters: This setup makes the broker and worker ready to accept client requests. - Worker reply handling: Trace how the worker listener responds to a request, including its reply destination and correlation ID.
Why it matters: These steps let the worker send a response to the destination identified by the request.
Key terms: start(), BrokerService, embedded broker, setupConsumer(), message listener, JMSReplyTo, correlation ID, reply-to queue
7.3.2 Implementing the client
This section implements a request/reply client that initializes JMS resources, sends requests, and listens for replies on a temporary queue.
- Client initiation and setup: Describe how the client begins request/reply and prepares to send messages.
Why it matters: The client starts the request/reply process and needs JMS resources before it can send a request. - Request and reply routing: Trace how the client prepares and sends a request and where it listens for the reply.
Why it matters: These message properties and queues establish the client side of the request/reply exchange.
Key terms: start(), connection, session, destination, producer, request(), request queue, temporary queue, JMSReplyTo, correlation ID
7.3.3 Running the request/reply example
This section runs the request/reply example, relates it to large-scale asynchronous messaging, and describes Spring’s benefit for JMS configuration.
- Running the request reply example: Describe how to start the request/reply example and what its client produces.
Why it matters: This gives you a way to run the example and recognize its expected result. - Scaling with asynchronous messaging: Explain how multiple brokers and workers support the large-scale request/reply scenario described in this section.
Why it matters: The example illustrates how asynchronous messaging can support both heavy loads and continuity when individual workers go down. - Spring JMS boilerplate reduction: Explain why the section recommends Spring when writing JMS applications.
Why it matters: This explains the motivation for using Spring with JMS clients.
Key terms: Server, Client, requests, replies, worker, asynchronous messaging, brokers, workers, redundancy, failover
7.4 Writing JMS clients using Spring
This introduction describes how ActiveMQ and Spring support common JMS client configuration tasks.
- ActiveMQ connection and destination configuration: Describe how ActiveMQ classes support configuring JMS connections and destinations for an application.
Why it matters: These configuration objects give JMS clients access to broker connections and destinations. - Spring JMS client helpers: Describe the roles of Spring's helper classes in configuring JMS consumers and producers.
Why it matters: Spring provides helpers for setting up both sides of a JMS client.
Key terms: connection factory, JMS destinations, beans, message listener container, message listeners, helper bean classes, producers
7.4.1 Configuring JMS connections
This section explains how to configure an ActiveMQ connection factory and optionally pool JMS connections and sessions using Spring.
- ActiveMQ connection factory role: Explain how an ActiveMQConnectionFactory relates to connecting a JMS application to the broker and how it can be used with Spring.
Why it matters: The connection factory provides the application's route to the ActiveMQ broker. - Pooled connection factory configuration: Describe what PooledConnectionFactory pools, why an application might use it, and how its connectionFactory property is configured in the example.
Why it matters: Pooling offers a connection setup for cases where performance requires reuse. - Commons Pool dependency: Describe the additional dependency needed to use the pooled connection factory and the two ways the section gives to supply it.
Why it matters: The pooled factory needs its external library available to the application.
Key terms: ActiveMQConnectionFactory, ActiveMQConnection, Spring XML, PooledConnectionFactory, connectionFactory, JMS connections, sessions, Apache Commons Pool, classpath, Maven
7.4.2 Configuring JMS destinations
This section explains how to predefine JMS destinations and why predefined topics can be useful when clients must authenticate.
- Predefined JMS destinations: Explain how JMS destinations can be predefined in activemq.xml and why doing so can be useful.
Why it matters: Predefining destinations gives an application a way to configure them in advance when authentication makes that useful.
Key terms: JMS destinations, activemq.xml, ActiveMQTopic, ActiveMQQueue, constructor injection, client authentication
7.4.3 Creating JMS consumers
This section explains how Spring message listener containers receive JMS messages and configure the portfolio example’s consumers.
- Message listener container role: Explain how a Spring message listener container separates message-receiving infrastructure from listener business logic.
Why it matters: This separation lets your listener focus on what to do with messages rather than the infrastructure needed to receive them. - Portfolio consumer container setup: Describe the basic container configuration in the portfolio example and what happens when the containers start.
Why it matters: These are the basic pieces needed to wire a Spring JMS consumer in the example.
Key terms: message listener container, MLC, message listener, broker, DefaultMessageListenerContainer, DMLC, connection factory, destination, connection pooling
7.4.4 Creating JMS producers
This section shows how to send JMS messages with Spring’s JmsTemplate and MessageCreator and configure the publisher’s dependencies.
- Spring message creation and sending: Explain how a MessageCreator works with JmsTemplate to send a JMS message, using the stock publisher as an example.
Why it matters: This separates message creation from Spring’s convenient sending operation. - Publisher configuration dependencies: Describe the dependencies configured for the Spring publisher and JmsTemplate.
Why it matters: The producer needs these references before it can publish through the template. - Connection pooling for sends: Explain why the section recommends a pooled connection factory when using JmsTemplate outside a Java EE container.
Why it matters: Pooling addresses the resource lifecycle that accompanies each template send outside a Java EE container.
Key terms: JmsTemplate, MessageCreator, createMessage(), send(), JMS map message, publisher, JMS destinations, connection factory, pooled connection factory, JmsTemplate.send()
7.4.5 Putting it all together
The section brings the Spring JMS stock portfolio example together, describes its startup and output, and notes how Spring simplifies the original application.
- Spring Example Startup: Describe how the example starts the broker and JMS clients and what appears as it runs.
Why it matters: This shows how the separately configured pieces become a running application. - Spring Example Simplification: Explain how the Spring example relates to the original stock portfolio application.
Why it matters: This illustrates that introducing Spring can simplify an application without replacing most of its existing logic.
Key terms: ActiveMQ broker, Spring application context, JMS clients, standard output, stock portfolio example, Spring utilities, Spring JMS
7.5 Summary
Where this is used later
- Using local JNDI to integrate ActiveMQ with Jetty (chapter 8) · The idea of embedding an ActiveMQ broker in a Java application.
- (chapter intro) (chapter 8) · Spring can start an embedded broker and provide access to JMS destinations.
- Horizontal scaling (chapter 10) · Horizontal scaling extends the idea of distributing work across hosts.
- Configuring ActiveMQ for high availability (chapter 10) · Provides context for configuring brokers for high availability.
- Deploying ActiveMQ for large numbers of concurrent applications (chapter 10) · Introduces the need to handle large numbers of concurrent applications.
- The sample web application (chapter 8) · Using Spring with JMS clients.
Concepts explained in this chapter
- request-response message exchangeA messaging pattern in which a requester sends a message and receives a corresponding reply.
- MessageListenerMessageListener.onMessage() is the JMS callback method invoked to deliver a message to an asynchronous consumer.
- embedded brokerAn embedded broker is a message broker hosted within an application rather than run as a separate service.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
