Skip to content

Commit 189bee7

Browse files
committed
Intermediate work
1 parent be967da commit 189bee7

15 files changed

Lines changed: 186 additions & 26 deletions

‎log-mdc-ndc/pom.xml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,30 @@
66
<version>0.0.1-SNAPSHOT</version>
77
<name>logmdcndc</name>
88
<description>tutorial on logging with MDC and NDC</description>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.4.1.RELEASE</version>
14+
</parent>
915

1016
<dependencies>
17+
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-test</artifactId>
25+
<scope>test</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.jayway.jsonpath</groupId>
29+
<artifactId>json-path</artifactId>
30+
<scope>test</scope>
31+
</dependency>
32+
1133
<!--log4j dependencies -->
1234
<dependency>
1335
<groupId>log4j</groupId>

‎log-mdc-ndc/src/main/java/com/baeldung/mdc/BusinessService.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
public interface BusinessService {
44

5-
void businessMethod(String transactionId);
5+
boolean transfer(Long amount);
66

77
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.mdc;
2+
3+
public class DefaultBusinessService implements BusinessService {
4+
5+
@Override
6+
public boolean transfer(Long amount) {
7+
try {
8+
Thread.sleep((long) (Math.random()*1000));
9+
} catch (InterruptedException e) {
10+
// should not happen
11+
}
12+
return Math.random() >= 0.5;
13+
}
14+
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.mdc;
2+
3+
public abstract class DelegateBusiness {
4+
5+
private BusinessService delegate = null;
6+
7+
public DelegateBusiness(BusinessService delegate) {
8+
super();
9+
this.delegate = delegate;
10+
}
11+
12+
13+
public DelegateBusiness() {
14+
this( new DefaultBusinessService() );
15+
}
16+
17+
public boolean transfer(Long amount) {
18+
preTransfer();
19+
boolean outcome = delegate.transfer(amount);
20+
postTransfer(outcome);
21+
return outcome;
22+
}
23+
24+
abstract protected void postTransfer(boolean outcome);
25+
26+
abstract protected void preTransfer();
27+
28+
29+
30+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.mdc;
2+
3+
import java.util.concurrent.Callable;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
7+
import org.apache.log4j.MDC;
8+
9+
import com.baeldung.mdc.log4j.Log4jBusinessService;
10+
11+
public class GenericApp {
12+
public static void main(String[] args) {
13+
14+
ExecutorService executor = Executors.newFixedThreadPool(3);
15+
TransactionFactory transactionFactory = new TransactionFactory();
16+
17+
for(int i=0; i<10; i++){
18+
19+
TransactionContext tx = transactionFactory.buildTransaction();
20+
21+
executor.submit(new Runnable() {
22+
@Override
23+
public void run() {
24+
new DelegateBusiness() {
25+
26+
@Override
27+
protected void preTransfer() {
28+
MDC.put("transaction.id", tx.getTransactionId());
29+
MDC.put("transaction.owner", tx.getOwner());
30+
MDC.put("transaction.createdAt", tx.getCreatedAt());
31+
}
32+
33+
@Override
34+
protected void postTransfer(boolean outcome) {
35+
// TODO Auto-generated method stub
36+
37+
}
38+
}.transfer(
39+
tx.getAmount()
40+
);
41+
}
42+
});
43+
}
44+
45+
}
46+
}

‎log-mdc-ndc/src/main/java/com/baeldung/mdc/TransactionContext.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ public class TransactionContext {
77
private String transactionId;
88
private String owner;
99
private Date createdAt;
10+
private Long amount;
1011

1112
public TransactionContext(String transactionId, String owner) {
1213
this.transactionId = transactionId;
1314
this.owner = owner;
1415
this.createdAt = new Date();
16+
this.amount = (long) (Math.random()*100);
1517
}
1618

1719
public String getOwner() {
@@ -26,4 +28,8 @@ public Date getCreatedAt() {
2628
return createdAt;
2729
}
2830

31+
public Long getAmount() {
32+
return amount;
33+
}
34+
2935
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.mdc;
2+
3+
public class TransactionFactory {
4+
5+
public TransactionContext buildTransaction() {
6+
TransactionContext ctx = new TransactionContext("" + Math.random(), "owner" + Math.random());
7+
return ctx;
8+
}
9+
10+
}

‎log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j/Log4jBusinessService.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ public class Log4jBusinessService implements BusinessService {
99
private final static Logger logger = Logger.getLogger(Log4jBusinessService.class);
1010

1111
@Override
12-
public void businessMethod(String transactionId) {
13-
logger.info("Executing transaction #" + transactionId );
12+
public boolean transfer(Long amount) {
13+
logger.info("Executing transaction #" + amount );
14+
return false;
1415
}
1516

1617
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
package com.baeldung.mdc.log4j;
22

3-
import org.apache.log4j.Logger;
43
import org.apache.log4j.MDC;
54

65
import com.baeldung.mdc.TransactionContext;
6+
import com.baeldung.mdc.TransactionFactory;
77

88
public class Log4jExecutor implements Runnable {
99

1010
public void run() {
1111

12-
String transactionId = "" + Math.random();
13-
String owner = "owner" + Math.random();
14-
TransactionContext ctx = new TransactionContext(transactionId, owner);
12+
TransactionContext ctx = new TransactionFactory().buildTransaction();
1513

16-
MDC.put("transaction.id", transactionId);
17-
MDC.put("transaction.owner", owner);
14+
MDC.put("transaction.id", ctx.getTransactionId());
15+
MDC.put("transaction.owner", ctx.getOwner());
1816
MDC.put("transaction.createdAt", ctx.getCreatedAt());
1917

20-
new Log4jBusinessService().businessMethod(ctx.getTransactionId());
18+
new Log4jBusinessService().transfer(ctx.getAmount());
2119
}
2220

2321
}

‎log-mdc-ndc/src/main/java/com/baeldung/mdc/log4j2/Log4j2BusinessService.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ public class Log4j2BusinessService implements BusinessService {
1212
private static final Logger logger = LogManager.getLogger(); //Log4j2BusinessService.class);
1313

1414
@Override
15-
public void businessMethod(String transactionId) {
16-
logger.info("Executing transaction #{}", transactionId );
15+
public boolean transfer(Long amount) {
16+
logger.info("Executing transaction #{}", amount );
17+
return false;
1718
}
1819

1920
}

0 commit comments

Comments
 (0)