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:

  • Create the non-exclusive queue
  • Add the topic subscription to the new queue
  • Modify the authentication server
  • Test the load balancing functionality

Prerequisites

Level

  • Beginner

Create the Non-Exclusive Queue

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:

  1. In your code editor, check out the developer-exercise-5 branch or in your command line, enter git checkout remotes/origin/bonus -f
  2. In your code editor, open the file LoginMessageReplier.java under auth-server > server.
    open the file LoginMessageReplier.java
    Line 50 contains the name of the queue “LOGIN_QUEUE”.
  3. Log in to Solace PubSub+ Cloud, select your service, and click Manage Service.
    How to Build a Simple Chat App with Solace
  4. Click Queues in the left side.
    Queues
  5. Click the + Create button to create a new queue.
  6. In the Create Queue dialog box, name the queue LOGIN_QUEUE and click Create.
    Create Queue
  7. In the Edit Queue Settings dialog box, change the Access Type to Non-Exclusive and click the Apply button.

    Exclusive queues are for fault tolerance while non-exclusive queues are for load balancing. Learn about queue access types.
    Simple Chat App with Solace
  8. In the Confirm dialog box, click Apply changes.
    Confirm

Add the Topic Subscription to the Queue

    1. Click on the new LOGIN_QUEUE queue and click the Subscription tab.
      Simple Chat App with Solace - LOGIN_QUEUE
  1. Click the + Subscription button to open the Create Subscription dialog box.
  2. In the Create Subscription dialog box, add LOGIN/MESSAGE/REQUEST as the topic subscription, press Enter, and click Create.

Modify the Authentication Server

You are going to modify the authentication server to consume from the login request instead of receiving messages via a topic subscription.

  1. Under the auth-server directory, open the LoginMessageReplier.java file.
  2. Enter the following code to set the endpoint properties. (Lines 90-91).
    //Add the Queue Logic Here
    final EndpointProperties endpointProps = new EndpointProperties();
  3. Enter the following code to set the queue permissions and access type (Lines 92-94).
    //Set queue permissions to ‘consume’ and access type to ‘non-exclusive’
    endpointProps.setPermission(EndpointProperties.PERMISSION_CONSUME);
    endpointProps.setAccessType(EndpointProperties.ACCESSTYPE_NONEXCLUSIVE);
  4. Enter the following code to create the queue object (Lines 96- 97).
    //Create the queue object locally
    final Queue queue = JCSMPFactory.onlyInstance().createQueue(QUEUE_NAME);
    
  5. Enter the following code to provision the queue to ensure that it binds to an existing queue (Lines 98-101).
    //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);
  6. Enter the following code to create consumer flow properties and the flow object (Lines 102-104).
    //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.

  7. Enter the following code to set the acknowledgment mode for the flow (Line 105-107).
    //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.

  8. Enter the following code to create the flow receiver and receive the messages (Lines 109-118).
    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.

Test the Load Balancing Functionality

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.

  1. In your code editor, type mvn spring-boot:run under the auth-server directory to run one instance of the authentication server of the Web application.
  2. Open a new terminal in your code editor and type mvn spring-boot:run under the auth-server directory to run the second instance of the authentication server of the Web application.
  3. Open two Web browsers and enter localhost:8081 for each of them to access the Web application.
  4. In one of the instances of the Web application, enter the valid username “ValidUser” and password “solace”.
    Chat App with Solace - password
    The login succeeds because this user is in the HashMapCredentialRepository.java file.
  5. In the other instances of the Web application, enter the username “user” and password “user”.
    Chat App with Solace - Web application
    This time, the login fails because this user is not in the HashMapCredentialRepository.java file.

Congratulations!

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

Hong Qiu

Hong is the Senior Community Content Manager at Solace, where he is responsible for helping build the Solace community and creating developer-focused content. Prior to Solace, he spent over 10 years at Adobe managing the Adobe Developer Connection portals and planning, creating, and sourcing content for them. Before Adobe, he was a senior technical writer at Nortel and a technical writer at Cognos (now IBM).