Using RabbitMQ as a multiprotocol message broker

Installing and using an external RabbitMQ as a full-featured message broker enables new technological opportunities and the design of a production-like infrastructure.

Getting ready

In this recipe, we will install RabbitMQ as a standalone server and configure it so it supports STOMP messages.

We will also update our WebSocket Spring configuration to rely on this full-featured message broker, instead of the internal simple message broker.

How to do it…

  1. From the Git Perspective in Eclipse, check out the v8.2.x branch this time.
  2. Two new Java projects have been added, and they must be imported. From Eclipse, select the File | Import… menu.
  3. The Import wizard opens so you can select a type of project within a hierarchy. Open the Maven category, select Existing Maven Projects option, and click on Next.
  4. The Import Maven Project wizard opens. As the root directory, select (or type) the workspace location (which should be <home-directory>/workspace).
  5. As shown in the following screenshot, select the following two pom.xml files: cloudstreetmarket-shared/pom.xml and cloudstreetmarket-websocket/pom.xml.
    How to do it…
  6. The two projects cloudstreetmarket-shared and cloudstreetmarket-websocket must show up in the project hierarchy.
  7. Target a runtime environment on the web module with the following instructions: In Eclipse, right-click on the cloudmarket-websocket project, select the Properties menu, in the navigation panel, select Targeted Runtimes. In the central window, check the tick of the Server Apache Tomcat v8.0.
  8. In the /app directory, the cloudstreetmarket.properties file has been updated. Reflect the changes in your file located in <home-directory>/app/cloudstreetmarket.properties.
  9. Run the Maven clean and Maven install commands on zipcloud-parent and then on cloudstreetmarket-parent, followed by a Maven | Update Project on all the modules.
  10. Running RabbitMQ in the way we want, requires us to download and install the product as a standalone product.
  11. Depending upon the configuration of the local machine, different ways of proceeding apply. You will find the appropriate links and installation guides on the RabbitMQ website: https://www.rabbitmq.com/download.html

    Tip

    If you are using Windows OS, please note that it is a prerequisite to download and install Erlang (http://www.erlang.org/download.html).

  12. Once RabbitMQ is installed and once its service is running, open your favourite web browser in order to check that RabbitMQ is running as a web console at the URL: http://localhost:15672 (like in the following screenshot).
    How to do it…

    Note

    We will come back to this later to set up the RabbitMQ configuration. For now, just remember that this console can be used to monitor messages and administrate connections, queues, topics, and exchanges.

  13. The RabbitMQ STOMP plugin needs to be activated. This is done from the rabbitmq_server-x.x.xsbin directory, by executing the command line:
    rabbitmq-plugins enable rabbitmq_stomp
  14. The following Maven dependencies have been added:
    <dependency>
      <groupId>org.springframework.amqp</groupId>
      <artifactId>spring-rabbit</artifactId>
      <version>1.4.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>io.projectreactor</groupId>
      <artifactId>reactor-core</artifactId>
      <version>2.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>io.projectreactor</groupId>
      <artifactId>reactor-net</artifactId>
      <version>2.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>io.projectreactor.spring</groupId>
      <artifactId>reactor-spring-context</artifactId>
      <version>2.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>4.0.31.Final</version>
    </dependency>
  15. In the dispatcher-servlet.xml of the cloudstreetmarket-api module, the following beans have been added making use of the rabbit namespace:
    <beans xmlns="http://www.sfw.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       ...
       xmlns:rabbit="http://www.sfw.org/schema/rabbit"
       xsi:schemaLocation="http://www.sfw.org/schema/beans
       ...
       http://www.sfw.org/schema/rabbit
      http://www.sfw.org/schema/rabbit/spring-rabbit-1.5.xsd">
        ...
      <rabbit:connection-factory id="connectionFactory" host="localhost" username="guest" password="guest" />
      <rabbit:admin connection-factory="connectionFactory" />
      <rabbit:template id="messagingTemplate" connection-factory="connectionFactory"/>
    </beans>
  16. In the csmcore-config.xml file (in cloudstreetmarket-core), the following beans have been added with the task namespace:
    <beans xmlns="http://www.sfw.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        ...
        xmlns:task=http://www.sfw.org/schema/task
        http://www.sfw.org/schema/task/spring-task-4.0.xsd">
        ...
        <task:annotation-driven scheduler="wsScheduler"/>
        <task:scheduler id="wsScheduler" pool-size="1000"/>
        <task:executor id="taskExecutor"/>
    </beans>
  17. Still in the Spring configuration side of things, our AnnotationConfig bean (the main configuration bean for cloudstreetmarket-api) has been added the two annotations:
    @EnableRabbit
    @EnableAsync
    public class AnnotationConfig {
    	...
    }
  18. Finally, the WebSocketConfig bean has been updated as well; especially the broker registration. We now make use of a StompBrokerRelay instead of a simples broker:
    @Configuration
    @ComponentScan("edu.zipcloud.cloudstreetmarket.api")
    @EnableWebSocketMessageBroker
    @EnableScheduling
    @EnableAsync
    public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    ...
        @Override
        public void configureMessageBroker(final MessageBrokerRegistry registry) {
         registry.setApplicationDestinationPrefixes( WEBAPP_PREFIX_PATH);
         registry.enableStompBrokerRelay(TOPIC_ROOT_PATH);
        }
    }

Tip

That's it! Everything is set to use RabbitMQ as external broker for our system. However, please note that if you try to start the server right now, the code will be expecting MySQL to be installed as well as the Redis Server. These two third-party systems are going to be detailed over the two next recipes.

How it works...

Using a full-featured message broker

In comparison to a simple message broker, using a full-featured message broker such as RabbitMQ provides interesting benefits, which we will discuss now.

Clusterability – RabbitMQ

A RabbitMQ broker is made of one or more Erlang nodes. Each of these nodes represent an instance of RabbitMQ in itself and can be started independently. Nodes can be linked with each other using the command line tool rabbitmqctl. For example, rabbitmqctl join_cluster [email protected] would actually connect one node to an existing cluster network. RabbitMQ nodes use cookies to communicate with one another. To be connected on the same cluster, two nodes must have the same cookie.

More STOMP message types

A full-featured message message broker (in comparison with a simple message broker) supports additional STOMP frame commands. For example, ACK and RECEIPT are not supported by simple message brokers.

StompMessageBrokerRelay

In the previous recipe, we talked about the flow that a message passes through in the Spring WebSocket engine. As shown with the following image, this flow is not affected at all when switching to an external message broker relay.

StompMessageBrokerRelay

Only the RabbitMQ external message broker shows up as an extra piece.BrokerMessageHandler (StompBrokerRelayMessageHandler) acts only as a proxy targeting a RabbitMQ node behind the scenes. Only one TCP connection is maintained between the StompBrokerRelay and its message broker. The StompBrokerRelay maintains the connection by sending heartbeat messages.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset