Skip to content

Message Publish Subscribe

regunathb edited this page Nov 5, 2012 · 14 revisions

The Trooper RabbitMQ library provides a convenient way to perform simple publish-subscribe operations.

Supported features

  • Publish/Consume String and Object messages - one in each method call
  • Specify a number of queue configurations - publisher/consumer does round-robin among these configurations. Useful when multiple Rabbit hosts are configured for high-availability, higher throughput
  • Publish/Consume does retries in case of failures until all configurations are exhausted. Useful to provide guaranteed publish/consume on any of the available configured Rabbit hosts

Maven artifacts

GroupID/Org ArtifactID/Name Description
org.trpr platform-core The small set of core classes common to all of Trooper.
org.trpr platform-integration APIs and interface definitions common to integration needs.
org.trpr integration-rabbitmq An implementation of the messaging integration API using RabbitMQ.

API Interface

The publisher interface offers these useful methods:

public void publish(Object message) throws MessagingException;
public void publishString(String message) throws MessagingException;

The consumer interface provides these methods:

public Object consume() throws MessagingException;
public String consumeString() throws MessagingException;

Common methods for both these interfaces are:

public int getQueueDepth() throws MessagingException;
public void closeConnections() throws MessagingException;

Sample configuration in Spring

The Spring XML beans to create and use the Publisher may be defined as:

<bean id="greetingMessagePublisher" class="org.trpr.platform.integration.impl.messaging.RabbitMQMessagePublisherImpl">
    <property name="rabbitMQConfigurations">
        <list>
	    <ref bean="greetingLocalQueueConfiguration" />
	    <ref bean="greetingRemoteQueueConfiguration" />
	</list>
    </property>
</bean>	
	
<bean id="greetingLocalQueueConfiguration" parent="commonQueueConfiguration">
    <property name="hostName" value="localhost" />
</bean>

<bean id="greetingRemoteQueueConfiguration" parent="commonQueueConfiguration">
    <property name="hostName" value="remotehost" />
</bean>

<bean id="commonQueueConfiguration" class="org.trpr.platform.integration.impl.messaging.RabbitMQConfiguration">
    <property name="userName" value="guest" />
    <property name="password" value="guest" />
    <property name="virtualHost" value="/" />
    <property name="exchangeName" value="myexchange" />
    <property name="queueName" value="greetingInput" />
    <property name="exchangeType" value="direct" />
    <property name="routingKey" value="greetingInput" />
    <property name="portNumber" value="5672" />
</bean>

Working example

Complete configuration and usage in a sample Trooper batch job is available here:

Trooper/examples/example-batch/src/main/resources/external/greetingWorkSchedulerJob/spring-batch-config.xml