Chapter 4. Connecting to ActiveMQ
ActiveMQ uses connectors and URI-based addressing to make broker connectivity straightforward and flexible. The chapter begins by explaining that a URI identifies not only the broker location, but also the protocol and optional settings used to communicate with it. Simple hierarchical URIs such as tcp://localhost:61616 are shown first, along with composite URIs used by higher-level protocols such as failover and static networking. A key point throughout is that the same URI structure is used on both the broker and client sides, and that query parameters can fine-tune behavior, although they must be written carefully because whitespace is not allowed.
The chapter then explores transport connectors, which handle client-to-broker communication over the network. It demonstrates how to configure multiple connectors in ActiveMQ’s XML configuration and how to adapt the stock portfolio example so it can connect through different transports. TCP is presented as the default, efficient, and reliable choice, while NIO is recommended when scalability or heavier traffic becomes important. UDP is covered as a faster but less reliable option that may help in firewall-constrained or time-sensitive environments, SSL is introduced for encrypted communication with the need for keystores and truststores, and HTTP/HTTPS is shown as a workaround for restrictive firewalls, though at a performance cost. The VM transport is also introduced as a non-network option for embedded brokers running inside the same JVM, offering better performance and simpler deployment.
Finally, the chapter broadens the view from single-broker connectivity to broker networks and resiliency. Static network connectors are used when broker addresses are known in advance, while failover provides automatic reconnection for clients if a broker becomes unavailable, improving robustness without changing application code. Dynamic discovery is then introduced through multicast and discovery transports, allowing brokers and clients to find each other automatically when addresses are not fixed. The peer connector simplifies networking of embedded brokers, and fanout allows a producer to send messages to multiple brokers at once. Overall, the chapter emphasizes that ActiveMQ’s connectivity model supports everything from simple local messaging to secure, scalable, and highly available distributed broker topologies.
Anatomy of a URI
A composite URI
Listing 4.1. Modifying stock portfolio publisher to support various connector URIs
Listing 4.2. Modifying stock portfolio consumer to support various connector URIs
Listing 4.3. Configuring the NIO transport connector
Producer sends messages using nio transport, consumer receives them using tcp transport
Application exchanges messages with embedded broker using vm transport
An example of a complex network of brokers topology
Two applications exchange messages using two brokers in a static network.
Two applications communicating using embedded brokers over peer protocol
Producer sends messages to multiple brokers using the fanout protocol
FAQ
What is a connector URI in ActiveMQ?
A connector URI is a URI used to address an ActiveMQ broker and describe how to connect to it. It typically includes a scheme, a host and port, and optional query parameters for extra configuration.What is the difference between transport connectors and network connectors?
Transport connectors are used for client-to-broker communication, while network connectors are used for broker-to-broker communication to form broker networks or clusters.Why are URIs important in ActiveMQ?
URIs provide a simple, flexible way to identify brokers and configure connections. ActiveMQ uses them to specify protocols, broker locations, and optional connector settings.What does a typical TCP connector URI look like?
A typical TCP connector URI looks liketcp://hostname:port?key=value. For example, tcp://localhost:61616 connects to a broker on localhost port 61616.
ActiveMQ in Action ebook for free