From 189bee78720d88d6c5e28c34157df28d6a27c3fd Mon Sep 17 00:00:00 2001 From: danidemi Date: Tue, 1 Nov 2016 22:49:32 +0100 Subject: [PATCH] Intermediate work --- log-mdc-ndc/pom.xml | 22 +++++++++ .../com/baeldung/mdc/BusinessService.java | 2 +- .../baeldung/mdc/DefaultBusinessService.java | 15 ++++++ .../com/baeldung/mdc/DelegateBusiness.java | 30 ++++++++++++ .../java/com/baeldung/mdc/GenericApp.java | 46 +++++++++++++++++++ .../com/baeldung/mdc/TransactionContext.java | 6 +++ .../com/baeldung/mdc/TransactionFactory.java | 10 ++++ .../mdc/log4j/Log4jBusinessService.java | 5 +- .../com/baeldung/mdc/log4j/Log4jExecutor.java | 12 ++--- .../mdc/log4j2/Log4j2BusinessService.java | 5 +- .../baeldung/mdc/log4j2/Log4j2Executor.java | 11 ++--- .../mdc/logback/LogbackBusinessService.java | 5 +- .../baeldung/mdc/logback/LogbackExecutor.java | 13 +++--- .../java/com/baeldung/web/Application.java | 12 +++++ .../com/baeldung/web/GreetingController.java | 18 ++++++++ 15 files changed, 186 insertions(+), 26 deletions(-) create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/web/Application.java create mode 100644 log-mdc-ndc/src/main/java/com/baeldung/web/GreetingController.java diff --git a/log-mdc-ndc/pom.xml b/log-mdc-ndc/pom.xml index 9f1d48cae4..7e973b940e 100644 --- a/log-mdc-ndc/pom.xml +++ b/log-mdc-ndc/pom.xml @@ -6,8 +6,30 @@ 0.0.1-SNAPSHOT logmdcndc tutorial on logging with MDC and NDC + + + org.springframework.boot + spring-boot-starter-parent + 1.4.1.RELEASE + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + com.jayway.jsonpath + json-path + test + + log4j diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java index dc33580aca..21d065dc90 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java @@ -2,6 +2,6 @@ package com.baeldung.mdc; public interface BusinessService { - void businessMethod(String transactionId); + boolean transfer(Long amount); } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java new file mode 100644 index 0000000000..1df6783185 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DefaultBusinessService.java @@ -0,0 +1,15 @@ +package com.baeldung.mdc; + +public class DefaultBusinessService implements BusinessService { + + @Override + public boolean transfer(Long amount) { + try { + Thread.sleep((long) (Math.random()*1000)); + } catch (InterruptedException e) { + // should not happen + } + return Math.random() >= 0.5; + } + +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java new file mode 100644 index 0000000000..ada8f79b62 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/DelegateBusiness.java @@ -0,0 +1,30 @@ +package com.baeldung.mdc; + +public abstract class DelegateBusiness { + + private BusinessService delegate = null; + + public DelegateBusiness(BusinessService delegate) { + super(); + this.delegate = delegate; + } + + + public DelegateBusiness() { + this( new DefaultBusinessService() ); + } + + public boolean transfer(Long amount) { + preTransfer(); + boolean outcome = delegate.transfer(amount); + postTransfer(outcome); + return outcome; + } + + abstract protected void postTransfer(boolean outcome); + + abstract protected void preTransfer(); + + + +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java new file mode 100644 index 0000000000..3eee17ba5d --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/GenericApp.java @@ -0,0 +1,46 @@ +package com.baeldung.mdc; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.apache.log4j.MDC; + +import com.baeldung.mdc.log4j.Log4jBusinessService; + +public class GenericApp { + public static void main(String[] args) { + + ExecutorService executor = Executors.newFixedThreadPool(3); + TransactionFactory transactionFactory = new TransactionFactory(); + + for(int i=0; i<10; i++){ + + TransactionContext tx = transactionFactory.buildTransaction(); + + executor.submit(new Runnable() { + @Override + public void run() { + new DelegateBusiness() { + + @Override + protected void preTransfer() { + MDC.put("transaction.id", tx.getTransactionId()); + MDC.put("transaction.owner", tx.getOwner()); + MDC.put("transaction.createdAt", tx.getCreatedAt()); + } + + @Override + protected void postTransfer(boolean outcome) { + // TODO Auto-generated method stub + + } + }.transfer( + tx.getAmount() + ); + } + }); + } + + } +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java index f5694d0df0..c6750edfda 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java @@ -7,11 +7,13 @@ public class TransactionContext { private String transactionId; private String owner; private Date createdAt; + private Long amount; public TransactionContext(String transactionId, String owner) { this.transactionId = transactionId; this.owner = owner; this.createdAt = new Date(); + this.amount = (long) (Math.random()*100); } public String getOwner() { @@ -26,4 +28,8 @@ public class TransactionContext { return createdAt; } + public Long getAmount() { + return amount; + } + } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java new file mode 100644 index 0000000000..4631c662c0 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionFactory.java @@ -0,0 +1,10 @@ +package com.baeldung.mdc; + +public class TransactionFactory { + + public TransactionContext buildTransaction() { + TransactionContext ctx = new TransactionContext("" + Math.random(), "owner" + Math.random()); + return ctx; + } + +} diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java index 4587f49be4..415e686cae 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java @@ -9,8 +9,9 @@ public class Log4jBusinessService implements BusinessService { private final static Logger logger = Logger.getLogger(Log4jBusinessService.class); @Override - public void businessMethod(String transactionId) { - logger.info("Executing transaction #" + transactionId ); + public boolean transfer(Long amount) { + logger.info("Executing transaction #" + amount ); + return false; } } \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java index fb949aa4ef..96fe63a3ed 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jExecutor.java @@ -1,23 +1,21 @@ package com.baeldung.mdc.log4j; -import org.apache.log4j.Logger; import org.apache.log4j.MDC; import com.baeldung.mdc.TransactionContext; +import com.baeldung.mdc.TransactionFactory; public class Log4jExecutor implements Runnable { public void run() { - String transactionId = "" + Math.random(); - String owner = "owner" + Math.random(); - TransactionContext ctx = new TransactionContext(transactionId, owner); + TransactionContext ctx = new TransactionFactory().buildTransaction(); - MDC.put("transaction.id", transactionId); - MDC.put("transaction.owner", owner); + MDC.put("transaction.id", ctx.getTransactionId()); + MDC.put("transaction.owner", ctx.getOwner()); MDC.put("transaction.createdAt", ctx.getCreatedAt()); - new Log4jBusinessService().businessMethod(ctx.getTransactionId()); + new Log4jBusinessService().transfer(ctx.getAmount()); } } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java index e9e0461a0a..d1a8dca518 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java @@ -12,8 +12,9 @@ public class Log4j2BusinessService implements BusinessService { private static final Logger logger = LogManager.getLogger(); //Log4j2BusinessService.class); @Override - public void businessMethod(String transactionId) { - logger.info("Executing transaction #{}", transactionId ); + public boolean transfer(Long amount) { + logger.info("Executing transaction #{}", amount ); + return false; } } \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java index b206638ceb..a686486e8e 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2Executor.java @@ -3,20 +3,19 @@ package com.baeldung.mdc.log4j2; import org.apache.logging.log4j.ThreadContext; import com.baeldung.mdc.TransactionContext; +import com.baeldung.mdc.TransactionFactory; public class Log4j2Executor implements Runnable { public void run() { - String transactionId = "" + Math.random(); - String owner = "owner" + Math.random(); - TransactionContext ctx = new TransactionContext(transactionId, owner); + TransactionContext ctx = new TransactionFactory().buildTransaction(); - ThreadContext.put("transaction.id", transactionId); - ThreadContext.put("transaction.owner", owner); + ThreadContext.put("transaction.id", ctx.getTransactionId()); + ThreadContext.put("transaction.owner", ctx.getOwner()); ThreadContext.put("transaction.createdAt", ctx.getCreatedAt().toString()); - new Log4j2BusinessService().businessMethod(ctx.getTransactionId()); + new Log4j2BusinessService().transfer(ctx.getAmount()); } } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackBusinessService.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackBusinessService.java index d15016c5c9..74f96ef15e 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackBusinessService.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackBusinessService.java @@ -10,8 +10,9 @@ public class LogbackBusinessService implements BusinessService { private static final Logger logger = LoggerFactory.getLogger(LogbackBusinessService.class); @Override - public void businessMethod(String transactionId) { - logger.info("Executing transaction #{}", transactionId); + public boolean transfer(Long amount) { + logger.info("Executing transaction #{}", amount); + return false; } } \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackExecutor.java b/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackExecutor.java index 1e53ef9585..614188b757 100644 --- a/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackExecutor.java +++ b/log-mdc-ndc/src/main/java/com/baeldung/mdc/logback/LogbackExecutor.java @@ -3,20 +3,21 @@ package com.baeldung.mdc.logback; import org.slf4j.MDC; import com.baeldung.mdc.TransactionContext; +import com.baeldung.mdc.TransactionFactory; public class LogbackExecutor implements Runnable { public void run() { - String transactionId = "" + Math.random(); - String owner = "owner" + Math.random(); - TransactionContext ctx = new TransactionContext(transactionId, owner); + TransactionContext ctx = new TransactionFactory().buildTransaction(); - MDC.put("transaction.id", transactionId); - MDC.put("transaction.owner", owner); + MDC.put("transaction.id", ctx.getTransactionId()); + MDC.put("transaction.owner", ctx.getOwner()); MDC.put("transaction.createdAt", ctx.getCreatedAt().toString()); - new LogbackBusinessService().businessMethod(ctx.getTransactionId()); + new LogbackBusinessService().transfer(ctx.getAmount()); } + + } diff --git a/log-mdc-ndc/src/main/java/com/baeldung/web/Application.java b/log-mdc-ndc/src/main/java/com/baeldung/web/Application.java new file mode 100644 index 0000000000..a700e345db --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/web/Application.java @@ -0,0 +1,12 @@ +package com.baeldung.web; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/log-mdc-ndc/src/main/java/com/baeldung/web/GreetingController.java b/log-mdc-ndc/src/main/java/com/baeldung/web/GreetingController.java new file mode 100644 index 0000000000..760e26a705 --- /dev/null +++ b/log-mdc-ndc/src/main/java/com/baeldung/web/GreetingController.java @@ -0,0 +1,18 @@ +package com.baeldung.web; + +import java.util.concurrent.atomic.AtomicLong; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class GreetingController { + + @GetMapping("/echo") + public String echo(@RequestBody String body) { + return body; + } +} \ No newline at end of file