-
-
Save marlly/4750601a91181b96e32f to your computer and use it in GitHub Desktop.
Example of annotation driven events using EventBus (Google Guava library)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.mockito.Mockito.verify; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.Spy; | |
import org.mockito.runners.MockitoJUnitRunner; | |
import com.google.common.eventbus.EventBus; | |
@RunWith(MockitoJUnitRunner.class) | |
public class TestAnnotationDrivenEvent { | |
private final EventBus eventBus = new EventBus(); | |
@Spy | |
private final MessageRecievedEventConsumer listener = new MessageRecievedEventConsumer(); | |
@Before | |
public void registerListener() { | |
eventBus.register(listener); | |
} | |
@Test | |
public void fireEvent() { | |
Message msg = new Message("marlly", "Interface vs annotation driven events", "Post about differences between those event architectures"); | |
MessageRecievedEvent msgEvent = new MessageRecievedEvent(msg); | |
eventBus.post(msgEvent); | |
verify(listener).messageRecieved(msgEvent); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Message { | |
private final String sender; | |
private final String subject; | |
private final String text; | |
public Message(String sender, String subject, String text) { | |
this.sender = sender; | |
this.subject = subject; | |
this.text = text; | |
} | |
public String getSender() { | |
return sender; | |
} | |
public String getSubject() { | |
return subject; | |
} | |
public String getText() { | |
return text; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MessageRecievedEvent { | |
private final Message msg; | |
public MessageRecievedEvent(Message msg) { | |
this.msg = msg; | |
} | |
public Message getMsg() { | |
return msg; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.google.common.eventbus.Subscribe; | |
public class MessageRecievedEventConsumer { | |
private static final Logger LOG = LoggerFactory.getLogger(MessageRecievedEventConsumer.class); | |
@Subscribe | |
public void messageRecieved(MessageRecievedEvent e) { | |
LOG.info("messageRecieved(), msg: {}", e.getMsg()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment