In this article we discuss how a client connects to a broker service. Client needs to know the broker URL to which it has to connect.
In the below code, we create a client connection.
ActiveMQConnectionFactory localFactory = new ActiveMQConnectionFactory(LOCAL_BROKER_TRANSPORT_URI); Connection clientConnection = localFactory.createConnection();
First we need a connection factory. We pass the broker URL to the factory constructor so that the connection factory is always for a specific broker. localFactory.createConnection() creates the client connection to the broker service.
Internally, a client socket is created to connect to the remote address where the broker is running.
After getting connected, input/output streams are created for receiving and sending commands. A TCP consumer thread is then started to read the stream into command and consume it.
After the client connects to the broker, first thing it does, is to send its preferred wire format info, provided the wire format is ‘OpenWireFormat‘. Likewise, broker sends its preferred wire format info. Unless this exchange takes place, the connection may not proceed further and may abort.
Once the wire format is received it is negotiated to come to a common wire format settings. If the wire format is not ‘OpenWireFormat‘ then we don’t need any wire format negotiations.
Broker then sends the broker info to the client.
In order to monitor the activity between client and broker, each keeps sending heart beats to the other. If any inactivity is noticed, the connection is aborted.
Once we have the client connection, we start it, so that the client synchronously sends its connection info to the broker. Since it is synchronously sent, it waits till it gets a response from the broker.
The connection ID will be used as the correlation of responses to requests, to create a blocking request-response.