DEV Community

Hong Qiu for Solace Developers

Posted on • Originally published at solace.com on

How to Build a Simple Chat App with Solace (Part 6)

Further thoughts on FPGA co-processing and performance

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. Line 50 contains the name of the queue “LOGIN_QUEUE”.
  3. Log in to Solace PubSub+ Cloud, select your service, and click Manage Service.
  4. Click Queues in the left side.
  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.
  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.

  1. In the Confirm dialog box, click Apply changes.

Add the Topic Subscription to the Queue

  1. Click on the new LOGIN_QUEUE queue and click the Subscription tab.
  2. Click the + Subscription button to open the Create Subscription dialog box.
  3. 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 Herefinal EndpointProperties endpointProps = new EndpointProperties();
Enter fullscreen mode Exit fullscreen mode
  1. 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);
Enter fullscreen mode Exit fullscreen mode
  1. Enter the following code to create the queue object (Lines 96- 97).
//Create the queue object locallyfinal Queue queue = JCSMPFactory.onlyInstance().createQueue(QUEUE\_NAME);
Enter fullscreen mode Exit fullscreen mode
  1. 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 existssession.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);
Enter fullscreen mode Exit fullscreen mode
  1. 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 Queuefinal ConsumerFlowProperties flowProps = new ConsumerFlowProperties();flowProps.setEndpoint(queue);
Enter fullscreen mode Exit fullscreen mode

The consumer flow properties define the queue that you would like to connect to.

  1. 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 callbackflowProps.setAckMode(JCSMPProperties.SUPPORTED\_MESSAGE\_ACK\_AUTO);
Enter fullscreen mode Exit fullscreen mode

There are two types of acknowledgment modes: AUTO and CLIENT. Learn more about acknowledgment modes.

  1. 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 consumercons.start();
Enter fullscreen mode Exit fullscreen mode

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”. 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”. 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

The post How to Build a Simple Chat App with Solace (Part 6) appeared first on Solace.

Top comments (0)