This is the sixth post in a series in which I walk through the creation of a simple chat app with Solace PubSub+. In the last part, I explained how to send a REST POST request as a request/reply message. In this final part, you will learn how to create the non-exclusive queue that will receive the login request and load balance them out to the authentication server. Specifically, you will:
You need to create a queue for our PubSub+ Cloud instance to receive and save the login request for the authentication server. To create the queue, follow the steps below:
git checkout remotes/origin/bonus -f
You are going to modify the authentication server to consume from the login request instead of receiving messages via a topic subscription.
//Add the Queue Logic Here final EndpointProperties endpointProps = new EndpointProperties();
//Set queue permissions to ‘consume’ and access type to ‘non-exclusive’ endpointProps.setPermission(EndpointProperties.PERMISSION_CONSUME); endpointProps.setAccessType(EndpointProperties.ACCESSTYPE_NONEXCLUSIVE);
//Create the queue object locally final Queue queue = JCSMPFactory.onlyInstance().createQueue(QUEUE_NAME);
//Actually provision the queue, and do not fail if it already exists session.provision(queue, endpointProps, JCSMPSession.FLAG_IGNORE_ALREADY_EXISTS); System.out.printf(“Attempting to bind to the queue ‘%s’ on the PubSub+ Broker.%n”, QUEUE_NAME);
//Create a Flow be able to bind to and consume messages from the Queue final ConsumerFlowProperties flowProps = new ConsumerFlowProperties(); flowProps.setEndpoint(queue);
The consumer flow properties define the queue that you would like to connect to.
//Set to ‘auto acknowledge’ where the API will ack back to Solace at the end of the //message received callback flowProps.setAckMode(JCSMPProperties.SUPPORTED_MESSAGE_ACK_AUTO);
There are two types of acknowledgment modes: AUTO and CLIENT. Learn more about acknowledgment modes.
final FlowReceiver cons = session.createFlow(new LoginRequestHandler(), flowProps, endpointProps, new FlowEventHandler() { @Override public void handleEvent(Object o, FlowEventArgs flowEventArgs) { System.out.println(o, toString() + “,” + flowEventArgs); } }) //Start the consumer cons.start();
This code snippet lets you handle events and print out the information to the console.
With the load balancing consumption pattern, your applications are not blocked when your replying applications cannot keep up with the number of requests.
To test the load balancing functionality of the login server, follow these steps to start multiple authentication server instances and see how they load balance the login requests. Note that there should be one web-server instance and two auth-server instances running for the final test. Also make sure that you have the right environment variables.
mvn spring-boot:run
under the auth-server directory to run one instance of the authentication server of the Web application.mvn spring-boot:run
under the auth-server directory to run the second instance of the authentication server of the Web application.You have successfully added code to bind to the login queue and respond to the request messages.
Not only that, you have reached the end of the series! To learn more about PubSub+ for developers, visit solace.dev. To share your developer experience using Solace PubSub+ or ask for help from other developers, visit the Solace Community.
Related