@UiHandler("callService") public void callService(ClickEvent e) { final RequestDispatcher dispatcher = ErraiBus.getDispatcher(); MessageBuilder.createMessage() .toSubject("IsDirtyService") .signalling() .noErrorHandling() .sendNowWith(dispatcher); }
public void testCallerUsesShadowServiceIfBusNotConnected() { runAfterInit( () -> { ((ClientMessageBusImpl) ErraiBus.get()).setState(BusState.CONNECTION_INTERRUPTED); final Greeter greeter = IOC.getBeanManager().lookupBean(Greeter.class).getInstance(); final CallerBean callerBean = IOC.getBeanManager().lookupBean(CallerBean.class).getInstance(); callerBean .getOnlineServiceCaller() .call( (r) -> { assertEquals(greeter.offline(), r); finishTest(); }) .greeting(); }); }
public void testShadowServiceAsMessageCallback() { runAfterInit( () -> { final OfflineMessageCallback callback = IOC.getBeanManager().lookupBean(OfflineMessageCallback.class).getInstance(); assertNull(callback.getGreeting()); MessageBuilder.createMessage() .toSubject("Greeting") .signalling() .with("greeting", "Hello, there") .noErrorHandling() .sendNowWith(ErraiBus.get()); assertNotNull(callback.getGreeting()); assertEquals("Hello, there", callback.getGreeting()); finishTest(); }); }
@Override public Sender provide(Class<?>[] typeargs, Annotation[] qualifiers) { String toSubject = null, replyTo = null; typeargs = typeargs == null ? new Class<?>[0] : typeargs; for (Annotation a : qualifiers) { if (a instanceof ToSubject) { toSubject = ((ToSubject) a).value(); } else if (a instanceof ReplyTo) { replyTo = ((ReplyTo) a).value(); } } if (typeargs.length != 1) { throw new ProviderException( PROVIDER_EXCEPTION_ERROR_MSG_BASE + ": Type at injection point must have exactly" + " one type parameter. (found: " + typeargs.length + ")"); } if (toSubject == null) { throw new ProviderException( PROVIDER_EXCEPTION_ERROR_MSG_BASE + ": Required " + ToSubject.class.getName() + " qualifier missing at injection point."); } if (typeargs.length != 1) { throw new ProviderException( PROVIDER_EXCEPTION_ERROR_MSG_BASE + ": Type at injection point must have exactly" + " one type parameter. (found: " + typeargs.length + ")"); } return ErraiMessageSender.of(toSubject, replyTo, ErraiBus.get()); }
/** * Interface to to the shoutbox service.<br> * Usage is different for providers <i>offering</i> and clients <i>demanding</i> a subject through * the shoutbox service. * * <p> * * <p>Provider's do <i>submit</i> or <i>retract</i> offers: <br> * * <pre> * Shoutbox shoutbox = new Shoutbox(); // stateful * shoutbox.submitOffer(PID, "demo.mailSender"); * <p/> * // provider becomes unavailable * shoutbox.retractOffer(PID, "demo.mailSender"); * </pre> * * <p> * * <p>Clients on the other hand do <i>engage</i> or <i>retire</i> offers: * * <pre> * shoutbox.engageOffer(CID, "demo.mailSender", * new ShoutboxCallback() * { * public void offerSubmitted(String providerId) * { * // provider becomes available * } * <p/> * public void offerRetracted(String providerId) * { * // provider retracted offer * // may be temporary * } * } * ); * <p/> * // client doesn't need the provider anymore * shoutbox.retireOffer(CID, "demo.mailSender"); * <p/> * </pre> * * @author Heiko Braun <*****@*****.**> * @see ShoutboxModule */ public class Shoutbox { private final MessageBus bus = ErraiBus.get(); private ShoutboxCallback delegate; public void submitOffer(String provider, String subjectMatter) { createMessage() .toSubject(ShoutboxModule.INBOX) .command(ShoutboxCmd.SUBMIT_OFFER) .with(ShoutboxCmdParts.SUBJECT, subjectMatter) .with(ShoutboxCmdParts.PROVIDER, provider) .noErrorHandling() .sendNowWith(bus); } public void retractOffer(String provider, String subjectMatter) { createMessage() .toSubject(ShoutboxModule.INBOX) .command(ShoutboxCmd.RETIRE_OFFER) .with(ShoutboxCmdParts.SUBJECT, subjectMatter) .with(ShoutboxCmdParts.PROVIDER, provider) .noErrorHandling() .sendNowWith(bus); } public void engageOffer(String client, String subject, ShoutboxCallback callback) { this.delegate = callback; // shout box example bus.subscribe( subject, new MessageCallback() { public void callback(Message message) { System.out.println("Shoutbox client: " + message.getCommandType()); switch (ShoutboxCmd.valueOf(message.getCommandType())) { case SUBMIT_OFFER: // provider enters the game delegate.offerSubmitted(message.get(String.class, ShoutboxCmdParts.PROVIDER)); break; case RETRACT_OFFER: delegate.offerRetracted(message.get(String.class, ShoutboxCmdParts.PROVIDER)); } } }); // engage an offer right away MessageBuilder.createMessage() .toSubject(ShoutboxModule.INBOX) .command(ShoutboxCmd.ENGAGE_OFFER) .with(ShoutboxCmdParts.SUBJECT, subject) .with(ShoutboxCmdParts.CLIENT, client) .noErrorHandling() .sendNowWith(bus); } public void retireOffer(String client, String subjectMatter) { MessageBuilder.createMessage() .toSubject(ShoutboxModule.INBOX) .command(ShoutboxCmd.RETIRE_OFFER) .with(ShoutboxCmdParts.SUBJECT, subjectMatter) .with(ShoutboxCmdParts.CLIENT, client) .noErrorHandling() .sendNowWith(bus); } }
public void onModuleLoad() { final HorizontalPanel hPanel = new HorizontalPanel(); final Label messagesReceived = new Label("Messaged Received: "); final Label messagesReceivedVal = new Label(); class Counter { int count = 0; public void increment() { messagesReceivedVal.setText(String.valueOf(++count)); } } final Counter counter = new Counter(); for (int i = 0; i < 7; i++) { final VerticalPanel panel = new VerticalPanel(); final Button startStopButton = new Button("Start" + i); final TextBox resultBox = new TextBox(); resultBox.setEnabled(false); final String receiverName = "RandomNumberReceiver" + i; final Style resultStyle = resultBox.getElement().getStyle(); /** Create a callback receiver to receive the data from the server. */ final MessageCallback receiver = new MessageCallback() { public void callback(Message message) { counter.increment(); Double value = message.get(Double.class, "Data"); resultBox.setText(String.valueOf(value)); if (value > 0.5d) { resultStyle.setProperty("backgroundColor", "green"); } else { resultStyle.setProperty("backgroundColor", "red"); } } }; /** Subscribe to the receiver using the recevierName. */ ErraiBus.get().subscribe(receiverName, receiver); final int num = i; startStopButton.addClickHandler( new ClickHandler() { public void onClick(ClickEvent clickEvent) { /** Send a message to Start/Stop the task on the server. */ MessageBuilder.createMessage() .toSubject("AsyncService") .command(startStopButton.getText()) .with(MessageParts.ReplyTo, receiverName) .noErrorHandling() .sendNowWith(ErraiBus.get()); /** * Flip-flop the value of the button every time it's pushed between 'Start' and 'Stop' */ startStopButton.setText( ("Start" + num).equals(startStopButton.getText()) ? "Stop" + num : "Start" + num); } }); panel.add(startStopButton); panel.add(resultBox); hPanel.add(panel); } final VerticalPanel outerPanel = new VerticalPanel(); outerPanel.add(hPanel); final HorizontalPanel messageCounter = new HorizontalPanel(); messageCounter.add(messagesReceived); messageCounter.add(messagesReceivedVal); outerPanel.add(messageCounter); RootPanel.get().add(outerPanel); }
public class ServerLocalEventIntegrationTest extends AbstractErraiCDITest { private MessageBus bus = ErraiBus.get(); public static final String SUCCESS = "SUCCESS"; public static final String FAILURE = "FAILURE"; private final int TIMEOUT = 30000; public ServerLocalEventIntegrationTest() { InitVotes.registerOneTimePreInitCallback( new Runnable() { @Override public void run() { setup(); } }); } @Override public String getModuleName() { return "org.jboss.errai.cdi.event.LocalEventTestModule"; } private void setup() { bus.unsubscribeAll(SUCCESS); bus.unsubscribeAll(FAILURE); bus.subscribe( SUCCESS, new MessageCallback() { @Override public void callback(Message message) { finishTest(); } }); bus.subscribe( FAILURE, new MessageCallback() { @Override public void callback(Message message) { fail(); } }); } public void testServerReceivesLocalEvent() throws Exception { delayTestFinish(TIMEOUT); MessageBuilder.createMessage("fireEventB").signalling().noErrorHandling().sendNowWith(bus); } public void testClientDoesNotReceiveLocalEvent() throws Exception { delayTestFinish(TIMEOUT); final long start = System.currentTimeMillis(); Container.$( new Runnable() { @Override public void run() { MessageBuilder.createMessage("fireEventA") .signalling() .noErrorHandling() .sendNowWith(bus); new Timer() { @Override public void run() { if (System.currentTimeMillis() - start > TIMEOUT - 500) { cancel(); finishTest(); } } }.scheduleRepeating(200); } }); } }
@Override public RequestDispatcher get() { return ErraiBus.getDispatcher(); }
@Override protected void gwtTearDown() throws Exception { timer.cancel(); ((ClientMessageBusImpl) ErraiBus.get()).removeAllUncaughtExceptionHandlers(); super.gwtTearDown(); }