Merge pull request #544 from kemitix/data-bus

[WIP] Data Bus
This commit is contained in:
Ilkka Seppälä
2017-04-22 15:17:31 +03:00
committed by GitHub
19 changed files with 894 additions and 0 deletions

1
data-bus/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

33
data-bus/README.md Normal file
View File

@ -0,0 +1,33 @@
---
layout: pattern
title: Data Bus
folder: data-bus
permalink: /patterns/data-bus/
categories: Architectural
tags:
- Java
- Difficulty-Intermediate
---
## Intent
Allows send of messages/events between components of an application
without them needing to know about each other. They only need to know
about the type of the message/event being sent.
![data bus pattern uml diagram](./etc/data-bus.urm.png "Data Bus pattern")
## Applicability
Use Data Bus pattern when
* you want your components to decide themselves which messages/events they want to receive
* you want to have many-to-many communication
* you want your components to know nothing about each other
## Related Patterns
Data Bus is similar to
* Mediator pattern with Data Bus Members deciding for themselves if they want to accept any given message
* Observer pattern but supporting many-to-many communication
* Publish/Subscribe pattern with the Data Bus decoupling the publisher and the subscriber

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

View File

@ -0,0 +1,77 @@
@startuml
package com.iluwatar.databus {
class AbstractDataType {
- dataBus : DataBus
+ AbstractDataType()
+ getDataBus() : DataBus
+ setDataBus(dataBus : DataBus)
}
~class App {
- log : Logger {static}
~ App()
+ main(args : String[]) {static}
}
class DataBus {
- INSTANCE : DataBus {static}
- listeners : Set<Member>
+ DataBus()
+ getInstance() : DataBus {static}
+ publish(event : DataType)
+ subscribe(member : Member)
+ unsubscribe(member : Member)
}
interface DataType {
+ getDataBus() : DataBus {abstract}
+ setDataBus(DataBus) {abstract}
}
interface Member {
+ accept(DataType) {abstract}
}
}
package com.iluwatar.databus.data {
class MessageData {
- message : String
+ MessageData(message : String)
+ getMessage() : String
+ of(message : String) : DataType {static}
}
class StartingData {
- when : LocalDateTime
+ StartingData(when : LocalDateTime)
+ getWhen() : LocalDateTime
+ of(when : LocalDateTime) : DataType {static}
}
class StoppingData {
- when : LocalDateTime
+ StoppingData(when : LocalDateTime)
+ getWhen() : LocalDateTime
+ of(when : LocalDateTime) : DataType {static}
}
}
package com.iluwatar.databus.members {
class CounterMember {
- log : Logger {static}
- name : String
+ CounterMember(name : String)
+ accept(data : DataType)
- handleEvent(data : MessageData)
}
class StatusMember {
- id : int
- log : Logger {static}
+ StatusMember(id : int)
+ accept(data : DataType)
- handleEvent(data : StartingData)
- handleEvent(data : StoppingData)
}
}
AbstractDataType --> "-dataBus" DataBus
DataBus --> "-INSTANCE" DataBus
DataBus --> "-listeners" Member
AbstractDataType ..|> DataType
MessageData --|> AbstractDataType
StartingData --|> AbstractDataType
StoppingData --|> AbstractDataType
CounterMember ..|> Member
StatusMember ..|> Member
@enduml

51
data-bus/pom.xml Normal file
View File

@ -0,0 +1,51 @@
<?xml version="1.0"?>
<!--
The MIT License
Copyright (c) 2014-2016 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<properties>
<lombok.version>1.16.14</lombok.version>
</properties>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.16.0-SNAPSHOT</version>
</parent>
<artifactId>data-bus</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,45 @@
/*
The MIT License (MIT)
Copyright (c) 2016 Paul Campbell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.iluwatar.databus;
/**
* Base for data to send via the Data-Bus.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class AbstractDataType implements DataType {
private DataBus dataBus;
@Override
public DataBus getDataBus() {
return dataBus;
}
@Override
public void setDataBus(DataBus dataBus) {
this.dataBus = dataBus;
}
}

View File

@ -0,0 +1,79 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus;
import com.iluwatar.databus.data.MessageData;
import com.iluwatar.databus.data.StartingData;
import com.iluwatar.databus.data.StoppingData;
import com.iluwatar.databus.members.MessageCollectorMember;
import com.iluwatar.databus.members.StatusMember;
import java.time.LocalDateTime;
/**
* The Data Bus pattern
* <p>
* <p>{@see http://wiki.c2.com/?DataBusPattern}</p>
* <p>
* <p>The Data-Bus pattern provides a method where different parts of an application may
* pass messages between each other without needing to be aware of the other's existence.</p>
* <p>Similar to the {@code ObserverPattern}, members register themselves with the {@link DataBus}
* and may then receive each piece of data that is published to the Data-Bus. The member
* may react to any given message or not.</p>
* <p>It allows for Many-to-Many distribution of data, as there may be any number of
* publishers to a Data-Bus, and any number of members receiving the data. All members
* will receive the same data, the order each receives a given piece of data, is an
* implementation detail.</p>
* <p>Members may unsubscribe from the Data-Bus to stop receiving data.</p>
* <p>This example of the pattern implements a Synchronous Data-Bus, meaning that
* when data is published to the Data-Bus, the publish method will not return until
* all members have received the data and returned.</p>
* <p>The {@link DataBus} class is a Singleton.</p>
* <p>Members of the Data-Bus must implement the {@link Member} interface.</p>
* <p>Data to be published via the Data-Bus must implement the {@link DataType} interface.</p>
* <p>The {@code data} package contains example {@link DataType} implementations.</p>
* <p>The {@code members} package contains example {@link Member} implementations.</p>
* <p>The {@link StatusMember} demonstrates using the DataBus to publish a message
* to the Data-Bus when it receives a message.</p>
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
class App {
public static void main(String[] args) {
final DataBus bus = DataBus.getInstance();
bus.subscribe(new StatusMember(1));
bus.subscribe(new StatusMember(2));
final MessageCollectorMember foo = new MessageCollectorMember("Foo");
final MessageCollectorMember bar = new MessageCollectorMember("Bar");
bus.subscribe(foo);
bus.publish(StartingData.of(LocalDateTime.now()));
bus.publish(MessageData.of("Only Foo should see this"));
bus.subscribe(bar);
bus.publish(MessageData.of("Foo and Bar should see this"));
bus.unsubscribe(foo);
bus.publish(MessageData.of("Only Bar should see this"));
bus.publish(StoppingData.of(LocalDateTime.now()));
}
}

View File

@ -0,0 +1,73 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus;
import java.util.HashSet;
import java.util.Set;
/**
* The Data-Bus implementation.
*
* <p>This implementation uses a Singleton.</p>
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class DataBus {
private static final DataBus INSTANCE = new DataBus();
private final Set<Member> listeners = new HashSet<>();
public static DataBus getInstance() {
return INSTANCE;
}
/**
* Register a member with the data-bus to start receiving events.
*
* @param member The member to register
*/
public void subscribe(final Member member) {
this.listeners.add(member);
}
/**
* Deregister a member to stop receiving events.
*
* @param member The member to deregister
*/
public void unsubscribe(final Member member) {
this.listeners.remove(member);
}
/**
* Publish and event to all members.
*
* @param event The event
*/
public void publish(final DataType event) {
event.setDataBus(this);
listeners.forEach(listener -> listener.accept(event));
}
}

View File

@ -0,0 +1,48 @@
/*
The MIT License (MIT)
Copyright (c) 2016 Paul Campbell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.iluwatar.databus;
/**
* Events are sent via the Data-Bus.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public interface DataType {
/**
* Returns the data-bus the event is being sent on.
*
* @return The data-bus
*/
DataBus getDataBus();
/**
* Set the data-bus the event will be sent on.
*
* @param dataBus The data-bus
*/
void setDataBus(DataBus dataBus);
}

View File

@ -0,0 +1,37 @@
/*
The MIT License (MIT)
Copyright (c) 2016 Paul Campbell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.iluwatar.databus;
import java.util.function.Consumer;
/**
* Members receive events from the Data-Bus.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public interface Member extends Consumer<DataType> {
void accept(DataType event);
}

View File

@ -0,0 +1,49 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus.data;
import com.iluwatar.databus.AbstractDataType;
import com.iluwatar.databus.DataType;
/**
* An event raised when a string message is sent.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class MessageData extends AbstractDataType {
private final String message;
public MessageData(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public static DataType of(final String message) {
return new MessageData(message);
}
}

View File

@ -0,0 +1,51 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus.data;
import com.iluwatar.databus.AbstractDataType;
import com.iluwatar.databus.DataType;
import java.time.LocalDateTime;
/**
* An event raised when applications starts, containing the start time of the application.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class StartingData extends AbstractDataType {
private final LocalDateTime when;
public StartingData(LocalDateTime when) {
this.when = when;
}
public LocalDateTime getWhen() {
return when;
}
public static DataType of(final LocalDateTime when) {
return new StartingData(when);
}
}

View File

@ -0,0 +1,51 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus.data;
import com.iluwatar.databus.AbstractDataType;
import com.iluwatar.databus.DataType;
import java.time.LocalDateTime;
/**
* An event raised when applications stops, containing the stop time of the application.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class StoppingData extends AbstractDataType {
private final LocalDateTime when;
public StoppingData(LocalDateTime when) {
this.when = when;
}
public LocalDateTime getWhen() {
return when;
}
public static DataType of(final LocalDateTime when) {
return new StoppingData(when);
}
}

View File

@ -0,0 +1,67 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus.members;
import com.iluwatar.databus.DataType;
import com.iluwatar.databus.Member;
import com.iluwatar.databus.data.MessageData;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
/**
* Receiver of Data-Bus events that collects the messages from each {@link MessageData}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class MessageCollectorMember implements Member {
private static final Logger LOGGER = Logger.getLogger(MessageCollectorMember.class.getName());
private final String name;
private List<String> messages = new ArrayList<>();
public MessageCollectorMember(String name) {
this.name = name;
}
@Override
public void accept(final DataType data) {
if (data instanceof MessageData) {
handleEvent((MessageData) data);
}
}
private void handleEvent(MessageData data) {
LOGGER.info(String.format("%s sees message %s", name, data.getMessage()));
messages.add(data.getMessage());
}
public List<String> getMessages() {
return Collections.unmodifiableList(messages);
}
}

View File

@ -0,0 +1,82 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus.members;
import com.iluwatar.databus.DataType;
import com.iluwatar.databus.Member;
import com.iluwatar.databus.data.MessageData;
import com.iluwatar.databus.data.StartingData;
import com.iluwatar.databus.data.StoppingData;
import java.time.LocalDateTime;
import java.util.logging.Logger;
/**
* Receiver of Data-Bus events.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class StatusMember implements Member {
private static final Logger LOGGER = Logger.getLogger(StatusMember.class.getName());
private final int id;
private LocalDateTime started;
private LocalDateTime stopped;
public StatusMember(int id) {
this.id = id;
}
@Override
public void accept(final DataType data) {
if (data instanceof StartingData) {
handleEvent((StartingData) data);
} else if (data instanceof StoppingData) {
handleEvent((StoppingData) data);
}
}
private void handleEvent(StartingData data) {
started = data.getWhen();
LOGGER.info(String.format("Receiver #%d sees application started at %s", id, started));
}
private void handleEvent(StoppingData data) {
stopped = data.getWhen();
LOGGER.info(String.format("Receiver #%d sees application stopping at %s", id, stopped));
LOGGER.info(String.format("Receiver #%d sending goodbye message", id));
data.getDataBus().publish(MessageData.of(String.format("Goodbye cruel world from #%d!", id)));
}
public LocalDateTime getStarted() {
return started;
}
public LocalDateTime getStopped() {
return stopped;
}
}

View File

@ -0,0 +1,52 @@
package com.iluwatar.databus;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.never;
/**
* Tests for {@link DataBus}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class DataBusTest {
@Mock
private Member member;
@Mock
private DataType event;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void publishedEventIsReceivedBySubscribedMember() {
//given
final DataBus dataBus = DataBus.getInstance();
dataBus.subscribe(member);
//when
dataBus.publish(event);
//then
then(member).should().accept(event);
}
@Test
public void publishedEventIsNotReceivedByMemberAfterUnsubscribing() {
//given
final DataBus dataBus = DataBus.getInstance();
dataBus.subscribe(member);
dataBus.unsubscribe(member);
//when
dataBus.publish(event);
//then
then(member).should(never()).accept(event);
}
}

View File

@ -0,0 +1,40 @@
package com.iluwatar.databus.members;
import com.iluwatar.databus.data.MessageData;
import com.iluwatar.databus.data.StartingData;
import org.junit.Assert;
import org.junit.Test;
import java.time.LocalDateTime;
/**
* Tests for {@link MessageCollectorMember}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class MessageCollectorMemberTest {
@Test
public void collectMessageFromMessageData() {
//given
final String message = "message";
final MessageData messageData = new MessageData(message);
final MessageCollectorMember collector = new MessageCollectorMember("collector");
//when
collector.accept(messageData);
//then
Assert.assertTrue(collector.getMessages().contains(message));
}
@Test
public void collectIgnoresMessageFromOtherDataTypes() {
//given
final StartingData startingData = new StartingData(LocalDateTime.now());
final MessageCollectorMember collector = new MessageCollectorMember("collector");
//when
collector.accept(startingData);
//then
Assert.assertEquals(0, collector.getMessages().size());
}
}

View File

@ -0,0 +1,57 @@
package com.iluwatar.databus.members;
import com.iluwatar.databus.DataBus;
import com.iluwatar.databus.data.MessageData;
import com.iluwatar.databus.data.StartingData;
import com.iluwatar.databus.data.StoppingData;
import org.junit.Assert;
import org.junit.Test;
import java.time.LocalDateTime;
import java.time.Month;
/**
* Tests for {@link StatusMember}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class StatusMemberTest {
@Test
public void statusRecordsTheStartTime() {
//given
final LocalDateTime startTime = LocalDateTime.of(2017, Month.APRIL, 1, 19, 9);
final StartingData startingData = new StartingData(startTime);
final StatusMember statusMember = new StatusMember(1);
//when
statusMember.accept(startingData);
//then
Assert.assertEquals(startTime, statusMember.getStarted());
}
@Test
public void statusRecordsTheStopTime() {
//given
final LocalDateTime stop = LocalDateTime.of(2017, Month.APRIL, 1, 19, 12);
final StoppingData stoppingData = new StoppingData(stop);
stoppingData.setDataBus(DataBus.getInstance());
final StatusMember statusMember = new StatusMember(1);
//when
statusMember.accept(stoppingData);
//then
Assert.assertEquals(stop, statusMember.getStopped());
}
@Test
public void statusIgnoresMessageData() {
//given
final MessageData messageData = new MessageData("message");
final StatusMember statusMember = new StatusMember(1);
//when
statusMember.accept(messageData);
//then
Assert.assertNull(statusMember.getStarted());
Assert.assertNull(statusMember.getStopped());
}
}

View File

@ -135,6 +135,7 @@
<module>event-asynchronous</module>
<module>queue-load-leveling</module>
<module>object-mother</module>
<module>data-bus</module>
<module>converter</module>
<module>guarded-suspension</module>
<module>balking</module>