added sampe code in documentation

This commit is contained in:
benni 2012-10-28 09:11:45 +01:00
parent dbbeabebc9
commit 591e107f4e
3 changed files with 67 additions and 1 deletions

View File

@ -24,6 +24,52 @@ a single message handler
mark and configure your message handlers with @Listener annotations and finally register the listeners at any Mbassador instance. Start
sending messages to your listeners using one of Mbassador's publication methods (sync or async). Done!
---------------------------------------
Usage:
Listener Definition (in any Bean):
// every event of type TestEvent or any subtype will be delivered
// to this handler
@Listener
public void handleTestEvent(TestEvent event) {
// do something
}
// this handler will be invoked asynchronously
@Listener(mode = Listener.Dispatch.Asynchronous)
public void handleSubTestEvent(SubTestEvent event) {
// do something more expensive here
}
// this handler will receive events of type SubTestEvent
// or any subtabe and that passes the given filter(s)
@Listener({@Filter(value = SpecialEventsOnly.class),@Filter(value = SpecialEventsOnly.class)})
public void handleFiltered(SubTestEvent event) {
//do something special here
}
Creation of message bus and registration of listeners:
// create as many instances as necessary
// bind it to any upper bound
MBassador<TestEvent> bus = new MBassador<TestEvent();
ListeningBean listener = new ListeningBean();
bus.subscribe(listener)
Message puclication:
TestEvent event = new TestEvent();
TestEvent subEvent = new SubTestEvent();
bus.publishAsync(event); //returns immediately, publication will continue asynchronously
bus.publish(subEvent); // will return after each handler has been invoked
---------------------------------------
Planned features:
+ Maven dependency: Add Mbassador to your project using maven. Coming soon!

View File

@ -28,4 +28,13 @@ public interface MessageFilter {
}
}
public static final class None implements MessageFilter {
@Override
public boolean accepts(Object event, Object listener) {
return false;
}
}
}

View File

@ -2,6 +2,8 @@ package org.mbassy;
import org.junit.Assert;
import org.junit.Test;
import org.mbassy.filter.Filter;
import org.mbassy.filter.MessageFilter;
import java.util.ArrayList;
import java.util.List;
@ -149,17 +151,26 @@ public class MBassadorTest {
public class EventingTestBean {
// every event of type TestEvent or any subtype will be delivered
// to this listener
@Listener
public void handleTestEvent(TestEvent event) {
event.counter.incrementAndGet();
}
// this handler will be invoked asynchronously
@Listener(mode = Listener.Dispatch.Asynchronous)
public void handleSubTestEvent(SubTestEvent event) {
event.counter.incrementAndGet();
}
// this handler will receive events of type SubTestEvent
// or any subtabe and that passes the given filter
@Listener({@Filter(MessageFilter.None.class),@Filter(MessageFilter.All.class)})
public void handleFiltered(SubTestEvent event) {
event.counter.incrementAndGet();
}
}