Thursday, March 11, 2010

DistributableEventPublisher for Spring via JGroups

Tagged with: , ,
Tuesday, December 11, 2007, 8:15
This news item was posted in HowTo & Tutorial category and has 0 Comments so far.

drawing6.pngMessaging is really easy through a single JVM. For a distributed environments JMS is strongly recommended. but for smaller and simple projects here is and example implementation via JGroups.
Usage

DistributableEventPublisher distributableEventPublisher=….
distributableEventPublisher.castEvent(TEXT_MESSAGE/XML/Serializable);

requirements:

  • springframework 2+
  • JGroups 2.6.1
  • JUnit (for test)

Java Files:

DistributableEvent simple implemetation of org.springframework.context.ApplicationEvent

public class DistributableEvent extends ApplicationEvent {
private Serializable event;
private Address src;

/**
* Create a new ApplicationEvent.
*
* @param source the component that published the event (never <code>null</code>)
*/
public DistributableEvent(Object source, Address src,Serializable event) {
super(source);
this.src=src;

this.event =event;
}

DistributableEventPublisher simple interface for event publisher

/** event dispatche for application context
* @author altuure
*/
public interface DistributableEventPublisher {
/**
* cast the event among peers
* @param serializable message
*/
void castEvent(Serializable serializable);
}

DistributableEventPublisherImpl implemnation of DistributableEventPublisher via JGroups

/**Event Dispatcher for spring context in a cluster
* @author altuure
*/
public class DistributableEventPublisherImpl
implements DistributableEventPublisher,
ApplicationEventPublisherAware,
InitializingBean,
DisposableBean,
RequestHandler {

private MessageDispatcher messageDispatcher;
private Channel channel;

private ApplicationEventPublisher applicationEventPublisher;
private String jgroupsConfig=null;
private String channelName;
private ReceiverAdapter receiverAdapter;
private Object lastMessage;
private boolean asynchronous =true;

/**
* last recieved message
* @return last message
*/
public Object getLastMessage() {
return lastMessage;
}

/**
* publish the event across application contexts
* @param serializable
*/

public void castEvent(Serializable serializable) {
messageDispatcher.castMessage(null, new Message(null, null, serializable), GroupRequest.GET_ALL, 0);

}

/**
* dispacth th incomimng message in the current application context
* @param msg incoming message
* @return null no value
*/
public Object handle(final Message msg) {
this.lastMessage = msg.getObject();
if (asynchronous) {
Runnable runnable = new Runnable() {
public void run() {
applicationEventPublisher.publishEvent(new DistributableEvent(this, msg.getSrc(), (Serializable) msg.getObject()));
}
};
new Thread(runnable).start();
} else {
applicationEventPublisher.publishEvent(new DistributableEvent(this, msg.getSrc(), (Serializable) msg.getObject()));

}
return null;
}

public void afterPropertiesSet() throws Exception {
channel=new JChannel(jgroupsConfig);
if(receiverAdapter!=null)
channel.setReceiver(receiverAdapter);
messageDispatcher= new MessageDispatcher(channel,null,null, this);
channel.connect(channelName);

}

public void destroy() throws Exception {
messageDispatcher.stop();
messageDispatcher=null;
channel.close();
channel=null;

}

and now spring configuration

<beans>

<bean id=”com.altuure.spring.jgroups.DistributableEventPublisher”
class=”com.altuure.spring.jgroups.DistributableEventPublisherImpl”>
<property name=”channelName” value=”App1″/>
<property name=”jgroupsConfig” value=”udp.xml”/>
<property name=”asynchronous” value=”true”/>

</bean>
</beans>

All comments are welcome :)

sources: projects sources

hope you enjoy it

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Wists
  • LinkedIn
  • Slashdot
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • email
  • Twitter

Related posts:

  1. light-weight distributed messaging via JGroups
  2. JGroups Basics
  3. JGroups-Spring In Action
  4. Clustering Acegi via JGroups (DistributedHashtable)
  5. hibernate tip: smart id generator

You can leave a response, or trackback from your own site.

Leave a Reply