Skip to content

RabbitExpress/RabbitEx-Java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

RabbitEx-Java & Scala

Version 2.0

Now with a scala backbone that can easily be used by scala apps and a Java wrapper with the same interface from 1.0

So you want to get into async messaging, but there's a lot to learn and it takes too long. While AMQP is a great protocol, it can be daunting for a development team unfamiliar with it. Well pick RabbitMQ and RabbitEx(press). This will allow you to get up and running using a very simple Tibco RVesque subject based addressing system. What's more is that it will be coming in multiple languages, so if your shop is full of polyglots, they can all use the same library.

Create a Connection

This is as simple as the following.

RabbitEx rabbitEx = new RabbitConnectionFactory().rabbitConnection(HOSTNAME, PORT);

Slightly more advanced would be using virtual hosts and authentication:

RabbitEx rabbitEx = new RabbitConnectionFactory().rabbitConnection(HOSTNAME, PORT, VIRTUAL_HOST, USERNAME, PASSWORD);

Using virtual hosts you can separate out environments while using the same Rabbit installation,

e.g. VIRTUAL_HOST = "development" or VIRTUAL_HOST = "test"

Note: The virtual host will need to be set up using the management console (default http://localhost:15672)

Publish a Message

In it's simplest form this is as follows:

rabbitEx.publish(EXCHANGE, SUBJECT, MESSAGES, null);

Exchanges are separations within the rabbit environment. This could be services (e.g. payments, email etc.). It is recommended not to use these for environmental separation (e.g. development and test), and instead use virtual hosts.

Subjects are used to separate messages within the exchange, but wildcards can be used (see later)

The 4th parameter, which is null here, can be used to set an "error exchange" and "error subject", which the consumer can use to notify of any errors processing a message.

Consume a Message

Again, very simple

Consumer consumer = rabbitEx.consume(EXCHANGE, SUBJECT, QUEUE, myHandler);
consumer.start();

myHandler is an object of a type that implements MessageHandler, e.g.

public class MyHandler implements MessageHandler {

  @Override
  public Response handleMessage(String message) {
    System.out.println("Message: " + message);
    return Response.ACK;
  }
}

The MessageHandler interface comes with three simple responses. Response.ACK which means everything is ok. Response.REJECT tells the system there was an issue (nack) and to reject the message Response.REQUEUE tells the system there was an issue and to requeue and try again

Either "REJECT" or "REQUEUE" will use (if sent with the publish call) the ERROR_EXCHANGE and ERROR_SUBJECT fields to send the error along with the action taken (REQUEUE or REJECT)

Queues and Wildcards

Subjects use dot '.' delimited phrases to specify a subject. For instance. 'library.books.fiction' A Queue can be bound to a specific subject, and will only receive messages that were sent to that exact subject phrase.

It is however possible to listen to multiple subjects and to do this we use wildcards.

'#' can be used as a wildcard to find anything in the next level up. e.g. 'library.#' would receive anything sent to library, or library.books, or library.magazines, but would not receive library.books.fiction

'*' can be used to get anything (no matter how many dots are used) so 'library.*' would get library, library.books, library.magazines and library.books.fiction

About

Simple Wrapper for Rabbit MQ pub/sub in Scala & Java

Resources

License

Stars

Watchers

Forks

Packages

No packages published