uk.org.retep.util.messaging.MessagingService Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2009, Peter T Mount
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the retep.org.uk nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package uk.org.retep.util.messaging;
import java.util.Collection;
import javax.annotation.Nonnull;
/**
* A MessagingService is the entry point for a Messaging system. Backed by
* a supplied Router implementation, it is used to submit messages and for
* attaching core Router's into the system.
*
* As components are added to the system, they are informed of the MessagingSystem
* instance which enables them to submit messages, specifically if they are
* replying to a message.
*
* @param Type of the address
* @param Type used to represent a route
* @author peter
* @since 9.2
*/
public final class MessagingService
{
private final Router router;
/**
* Constructor
*
* @param router Router to use as the core router.
* @throws uk.org.retep.util.messaging.MessageException
*/
public MessagingService( @Nonnull final Router router )
throws MessageException
{
this.router = router;
router.setMessagingService( this );
}
/**
* Add a component to this router by using the component's key.
* If the router already has a component for that key then that
* component will be asked to add it.
* @param component
* @throws uk.org.retep.util.messaging.MessageException if the
* component does not have a key or if it could not be added
*/
public void add( @Nonnull final Component> component )
throws MessageException
{
router.add( component );
}
/**
* Adds a Route to a Component.
*
* @param route Route to add
* @param component Component to use for this Route
* @return true if the Route was added
* @throws uk.org.retep.util.messaging.MessageException
* @see Router#addRoute(java.lang.Object, uk.org.retep.util.messaging.Component)
*/
public boolean addRoute( @Nonnull final R route,
@Nonnull final Component component )
throws MessageException
{
return router.addRoute( route, component );
}
/**
* Remove an individual Route
*
* @param route Route to add
* @return true if the Route was added
* @see Router#removeComponent(java.lang.Object)
*/
public boolean removeRoute( @Nonnull final R route )
{
return router.removeRoute( route );
}
/**
* Remove all Routes for this Component from the core Router
*
* @param component Component to remove
* @return true if the Component was removed
* @see Router#removeComponent(uk.org.retep.util.messaging.Component)
*/
public boolean removeComponent( @Nonnull final Component component )
{
return router.removeComponent( component );
}
/**
* Return a Collection of Routes attached to this router
* @return Collection<R>
*/
@Nonnull
public Collection getRoutes()
{
return router.getRoutes();
}
/**
* Return a Collection of Components attached to this router
* @return Collection<Component>
*/
@Nonnull
public Collection> getComponents()
{
return router.getComponents();
}
/**
* Send a Message. Once it has been submitted, the Message object must not
* be changed in any way. Ownership of that object passes to the MessagingSystem.
* @param message Message to submit
* @throws uk.org.retep.util.messaging.MessageException
*/
public void send( @Nonnull final Message message )
throws MessageException
{
router.consume( message );
}
/**
* Starts the service. This will pass down throughout the system ensuring
* that all Component's and Router's have been started
* @throws uk.org.retep.util.messaging.MessageException
*/
public void startService()
throws MessageException
{
router.startComponent();
}
/**
* Stops the service. This will pass down throughout the system ensuring
* that all Component's and Router's have been stopped.
*/
public void stopService()
{
router.stopComponent();
}
/**
* Has the MessagingSystem been started?
*
* @return true if started
*/
public boolean isServiceStarted()
{
return router.isComponentStarted();
}
}