mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-14 18:18:45 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
1 parent
371439aeaa
commit
0ca162a55c
1863 files changed
+14403
-17632
No files matched your search
@@ -34,6 +34,14 @@
|
||||
</parent>
|
||||
<artifactId>poison-pill</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
|
||||
@@ -50,11 +50,13 @@ public class App {
|
||||
|
||||
new Thread(consumer::consume).start();
|
||||
|
||||
new Thread(() -> {
|
||||
producer.send("hand shake");
|
||||
producer.send("some very important information");
|
||||
producer.send("bye!");
|
||||
producer.stop();
|
||||
}).start();
|
||||
new Thread(
|
||||
() -> {
|
||||
producer.send("hand shake");
|
||||
producer.send("some very important information");
|
||||
producer.send("bye!");
|
||||
producer.stop();
|
||||
})
|
||||
.start();
|
||||
}
|
||||
}
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.poison.pill;
|
||||
import com.iluwatar.poison.pill.Message.Headers;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Class responsible for receiving and handling submitted to the queue messages.
|
||||
*/
|
||||
/** Class responsible for receiving and handling submitted to the queue messages. */
|
||||
@Slf4j
|
||||
public class Consumer {
|
||||
|
||||
@@ -41,9 +39,7 @@ public class Consumer {
|
||||
this.queue = queue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume message.
|
||||
*/
|
||||
/** Consume message. */
|
||||
public void consume() {
|
||||
while (true) {
|
||||
try {
|
||||
|
||||
@@ -32,44 +32,43 @@ import java.util.Map;
|
||||
*/
|
||||
public interface Message {
|
||||
|
||||
Message POISON_PILL = new Message() {
|
||||
Message POISON_PILL =
|
||||
new Message() {
|
||||
|
||||
@Override
|
||||
public void addHeader(Headers header, String value) {
|
||||
throw poison();
|
||||
}
|
||||
@Override
|
||||
public void addHeader(Headers header, String value) {
|
||||
throw poison();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHeader(Headers header) {
|
||||
throw poison();
|
||||
}
|
||||
@Override
|
||||
public String getHeader(Headers header) {
|
||||
throw poison();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Headers, String> getHeaders() {
|
||||
throw poison();
|
||||
}
|
||||
@Override
|
||||
public Map<Headers, String> getHeaders() {
|
||||
throw poison();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBody(String body) {
|
||||
throw poison();
|
||||
}
|
||||
@Override
|
||||
public void setBody(String body) {
|
||||
throw poison();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBody() {
|
||||
throw poison();
|
||||
}
|
||||
@Override
|
||||
public String getBody() {
|
||||
throw poison();
|
||||
}
|
||||
|
||||
private RuntimeException poison() {
|
||||
return new UnsupportedOperationException("Poison");
|
||||
}
|
||||
private RuntimeException poison() {
|
||||
return new UnsupportedOperationException("Poison");
|
||||
}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Enumeration of Type of Headers.
|
||||
*/
|
||||
/** Enumeration of Type of Headers. */
|
||||
enum Headers {
|
||||
DATE, SENDER
|
||||
DATE,
|
||||
SENDER
|
||||
}
|
||||
|
||||
void addHeader(Headers header, String value);
|
||||
|
||||
@@ -27,6 +27,4 @@ package com.iluwatar.poison.pill;
|
||||
/**
|
||||
* Represents abstraction of channel (or pipe) that bounds {@link Producer} and {@link Consumer}.
|
||||
*/
|
||||
public interface MessageQueue extends MqPublishPoint, MqSubscribePoint {
|
||||
|
||||
}
|
||||
public interface MessageQueue extends MqPublishPoint, MqSubscribePoint {}
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.poison.pill;
|
||||
|
||||
/**
|
||||
* Endpoint to publish {@link Message} to queue.
|
||||
*/
|
||||
/** Endpoint to publish {@link Message} to queue. */
|
||||
public interface MqPublishPoint {
|
||||
|
||||
void put(Message msg) throws InterruptedException;
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.poison.pill;
|
||||
|
||||
/**
|
||||
* Endpoint to retrieve {@link Message} from queue.
|
||||
*/
|
||||
/** Endpoint to retrieve {@link Message} from queue. */
|
||||
public interface MqSubscribePoint {
|
||||
|
||||
Message take() throws InterruptedException;
|
||||
|
||||
@@ -39,22 +39,19 @@ public class Producer {
|
||||
private final String name;
|
||||
private boolean isStopped;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
/** Constructor. */
|
||||
public Producer(String name, MqPublishPoint queue) {
|
||||
this.name = name;
|
||||
this.queue = queue;
|
||||
this.isStopped = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send message to queue.
|
||||
*/
|
||||
/** Send message to queue. */
|
||||
public void send(String body) {
|
||||
if (isStopped) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"Producer %s was stopped and fail to deliver requested message [%s].", body, name));
|
||||
throw new IllegalStateException(
|
||||
String.format(
|
||||
"Producer %s was stopped and fail to deliver requested message [%s].", body, name));
|
||||
}
|
||||
var msg = new SimpleMessage();
|
||||
msg.addHeader(Headers.DATE, new Date().toString());
|
||||
@@ -69,9 +66,7 @@ public class Producer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop system by sending poison pill.
|
||||
*/
|
||||
/** Stop system by sending poison pill. */
|
||||
public void stop() {
|
||||
isStopped = true;
|
||||
try {
|
||||
|
||||
@@ -28,9 +28,7 @@ import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* {@link Message} basic implementation.
|
||||
*/
|
||||
/** {@link Message} basic implementation. */
|
||||
public class SimpleMessage implements Message {
|
||||
|
||||
private final Map<Headers, String> headers = new HashMap<>();
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.poison.pill;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
/**
|
||||
* Bounded blocking queue wrapper.
|
||||
*/
|
||||
/** Bounded blocking queue wrapper. */
|
||||
public class SimpleMessageQueue implements MessageQueue {
|
||||
|
||||
private final BlockingQueue<Message> queue;
|
||||
|
||||
@@ -24,17 +24,15 @@
|
||||
*/
|
||||
package com.iluwatar.poison.pill;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Application test */
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
@@ -37,10 +37,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* ConsumerTest
|
||||
*
|
||||
*/
|
||||
/** ConsumerTest */
|
||||
class ConsumerTest {
|
||||
|
||||
private InMemoryAppender appender;
|
||||
@@ -57,12 +54,12 @@ class ConsumerTest {
|
||||
|
||||
@Test
|
||||
void testConsume() throws Exception {
|
||||
final var messages = List.of(
|
||||
createMessage("you", "Hello!"),
|
||||
createMessage("me", "Hi!"),
|
||||
Message.POISON_PILL,
|
||||
createMessage("late_for_the_party", "Hello? Anyone here?")
|
||||
);
|
||||
final var messages =
|
||||
List.of(
|
||||
createMessage("you", "Hello!"),
|
||||
createMessage("me", "Hi!"),
|
||||
Message.POISON_PILL,
|
||||
createMessage("late_for_the_party", "Hello? Anyone here?"));
|
||||
|
||||
final var queue = new SimpleMessageQueue(messages.size());
|
||||
for (final var message : messages) {
|
||||
@@ -79,7 +76,7 @@ class ConsumerTest {
|
||||
/**
|
||||
* Create a new message from the given sender with the given message body
|
||||
*
|
||||
* @param sender The sender's name
|
||||
* @param sender The sender's name
|
||||
* @param message The message body
|
||||
* @return The message instance
|
||||
*/
|
||||
@@ -108,5 +105,4 @@ class ConsumerTest {
|
||||
return log.stream().map(ILoggingEvent::getFormattedMessage).anyMatch(message::equals);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,15 +30,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* PoisonMessageTest
|
||||
*
|
||||
*/
|
||||
/** PoisonMessageTest */
|
||||
class PoisonMessageTest {
|
||||
|
||||
@Test
|
||||
void testAddHeader() {
|
||||
assertThrows(UnsupportedOperationException.class, () -> POISON_PILL.addHeader(Headers.SENDER, "sender"));
|
||||
assertThrows(
|
||||
UnsupportedOperationException.class, () -> POISON_PILL.addHeader(Headers.SENDER, "sender"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -60,5 +58,4 @@ class PoisonMessageTest {
|
||||
void testGetBody() {
|
||||
assertThrows(UnsupportedOperationException.class, POISON_PILL::getBody);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,10 +35,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
/**
|
||||
* ProducerTest
|
||||
*
|
||||
*/
|
||||
/** ProducerTest */
|
||||
class ProducerTest {
|
||||
|
||||
@Test
|
||||
@@ -76,11 +73,11 @@ class ProducerTest {
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e);
|
||||
assertNotNull(e.getMessage());
|
||||
assertEquals("Producer Hello! was stopped and fail to deliver requested message [producer].",
|
||||
assertEquals(
|
||||
"Producer Hello! was stopped and fail to deliver requested message [producer].",
|
||||
e.getMessage());
|
||||
}
|
||||
|
||||
verifyNoMoreInteractions(publishPoint);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,10 +32,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* SimpleMessageTest
|
||||
*
|
||||
*/
|
||||
/** SimpleMessageTest */
|
||||
class SimpleMessageTest {
|
||||
|
||||
@Test
|
||||
@@ -55,6 +52,7 @@ class SimpleMessageTest {
|
||||
void testUnModifiableHeaders() {
|
||||
final var message = new SimpleMessage();
|
||||
final var headers = message.getHeaders();
|
||||
assertThrows(UnsupportedOperationException.class, () -> headers.put(Message.Headers.SENDER, "test"));
|
||||
assertThrows(
|
||||
UnsupportedOperationException.class, () -> headers.put(Message.Headers.SENDER, "test"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user