How to use RRiBbit

So, how do you actually use RRiBbit? Well, it's quite simple. First, simply annotate the methods that you want to be listeners with the @Listener annotation. You can see examples of this in the Introduction. Please see the FAQ for some caveats.

Once you have your annotated methods, you have to tell RRiBbit about them. RRiBbit uses so-called ListenerObjects internally to manage your listeners, so you have to create a ListenerObjectCreator that can find your listeners. There are several available, for object-based, class-based, package-based and spring-bean-based creation. You can find extensive information about all of them in the Javadocs. Let's assume for now that we want a InstantiatingClassBasedListenerObjectCreator, that scans some classes for methods with an @Listener annotation and attempts to create instances of those classes to run those methods on:

ListenerObjectCreator creator = new InstantiatingClassBasedListenerObjectCreator(ClassA.class, ClassB.class);

Awesome, the listeners are created!

Now, assuming that you want to use RRiBbit locally, meaning that you want to use it within your Java application, but you do not want to call listeners on other machines, the next step is really simple. Just pass your newly created ListenerObjectCreator to the RRiBbitUtil and you're good to go!

RRiBbitUtil.createRequestResponseBusForLocalUse(creator, true);

This method will set RRiBbit up for local use and will return the resulting RequestResponseBus. That same RequestResponseBus may also be retrieved from anywhere in your application via the static method RRB.get(). You can now send requests to it and they will be processed by your listeners. Simple as that!

The defaults that RRiBbitUtil has chosen include multithreading support, for concurrently executing listeners and caching, for better performance. Please note that using multithreading can result in the loss of your transactional context, when you have one. See the next chapter for information on how to use single threaded execution.

RRiBbit and Spring

RRiBbit and Spring truly are a match made in heaven. To use RRiBbit in combination with Spring, you have to use the SpringBeanClassBasedListenerObjectCreator, that uses your Spring beans as target objects for the listener executions and define it in your Spring context:

<bean id="creator" class="org.rribbit.creation.SpringBeanClassBasedListenerObjectCreator">
        <property name="classNames">
                <list>
                        <value>fully.qualified.name.of.ClassA</value>
                        <value>fully.qualified.name.of.ClassB</value>
                </list>
        </property>
</bean>

<bean id="requestResponseBus" class="org.rribbit.util.RRiBbitUtil" factory-method="createRequestResponseBusForLocalUse">
        <constructor-arg ref="creator" />
        <constructor-arg value="true" /> 
</bean>

And that's it! Now your spring beans (the ones implementing ClassA and ClassB interfaces) will be used to process your requests, including all Spring goodness that comes with them, such as AOP, Transactions, etc. You could also specify whole packages to be scanned instead of individual classes. This could be useful if you have many classes.

If you would like to know more about the internals of RRiBbit, or if you would like to know how to choose other objects than the defaults, then please continue in the next chapter: RRiBbit Internals.

RRiBbit and Maven

RRiBbit is available on the Maven Central. If you want to use it in your project, simply add this to your pom.xml (replace [version] by the version you want to use).

<dependency>
        <groupId>org.rribbit</groupId>
        <artifactId>rribbit</artifactId>
        <version>[version]</version>
        <type>jar</type>
</dependency>