Skip to content

Commit 2fe2e2a

Browse files
sunilmogadatiEugen
authored andcommitted
BAEL-40: Add NDC and JBoss Logging to the demo application (eugenp#880)
* Add NDC and JBoss Logging to the demo application * NDC for Log4j, Log4j2 and JBoss Logging * Simplify NDC example by making it a single operation instead of two * Make NDC example as RestController, Use JBoss Logging only as a logging bridge * Fix merge conflicts in pull request - log-mdc pom.xml updated
1 parent 8844871 commit 2fe2e2a

14 files changed

Lines changed: 457 additions & 17 deletions

‎log-mdc/pom.xml‎

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,37 @@
55
<artifactId>logmdc</artifactId>
66
<version>0.0.1-SNAPSHOT</version>
77
<name>logmdc</name>
8-
<description>tutorial on logging with MDC</description>
8+
<packaging>war</packaging>
9+
<description>tutorial on logging with MDC and NDC</description>
910

1011
<dependencies>
1112

12-
<dependency>
13-
<groupId>org.springframework</groupId>
14-
<artifactId>spring-context</artifactId>
15-
<version>${springframework.version}</version>
16-
</dependency>
17-
<dependency>
18-
<groupId>org.springframework</groupId>
19-
<artifactId>spring-webmvc</artifactId>
20-
<version>${springframework.version}</version>
21-
</dependency>
13+
<dependency>
14+
<groupId>org.springframework</groupId>
15+
<artifactId>spring-core</artifactId>
16+
<version>${springframework.version}</version>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-web</artifactId>
21+
<version>${springframework.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework</groupId>
25+
<artifactId>spring-webmvc</artifactId>
26+
<version>${springframework.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>javax.servlet</groupId>
30+
<artifactId>javax.servlet-api</artifactId>
31+
<version>${javax.servlet.version}</version>
32+
<scope>provided</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.fasterxml.jackson.core</groupId>
36+
<artifactId>jackson-databind</artifactId>
37+
<version>${jackson.library}</version>
38+
</dependency>
2239

2340
<!--log4j dependencies -->
2441
<dependency>
@@ -31,12 +48,12 @@
3148
<dependency>
3249
<groupId>org.apache.logging.log4j</groupId>
3350
<artifactId>log4j-api</artifactId>
34-
<version>2.7</version>
51+
<version>${log4j2.version}</version>
3552
</dependency>
3653
<dependency>
3754
<groupId>org.apache.logging.log4j</groupId>
3855
<artifactId>log4j-core</artifactId>
39-
<version>${log4j-api.version}</version>
56+
<version>${log4j2.version}</version>
4057
</dependency>
4158

4259
<!--disruptor for log4j2 async logging -->
@@ -52,22 +69,65 @@
5269
<artifactId>logback-classic</artifactId>
5370
<version>${logback.version}</version>
5471
</dependency>
72+
73+
<!-- JBoss Logging (bridge) dependencies -->
74+
<dependency>
75+
<groupId>org.jboss.logging</groupId>
76+
<artifactId>jboss-logging</artifactId>
77+
<version>${jbosslogging.version}</version>
78+
</dependency>
5579

5680
<dependency>
5781
<groupId>junit</groupId>
5882
<artifactId>junit</artifactId>
5983
<version>${junit.version}</version>
6084
<scope>test</scope>
6185
</dependency>
86+
<dependency>
87+
<groupId>org.springframework</groupId>
88+
<artifactId>spring-test</artifactId>
89+
<version>${springframework.version}</version>
90+
<scope>test</scope>
91+
</dependency>
6292

6393
</dependencies>
6494

6595
<properties>
6696
<springframework.version>4.3.4.RELEASE</springframework.version>
6797
<log4j.version>1.2.17</log4j.version>
68-
<log4j-api.version>2.7</log4j-api.version>
98+
<log4j2.version>2.7</log4j2.version>
6999
<disruptor.version>3.3.6</disruptor.version>
70100
<logback.version>1.1.7</logback.version>
101+
<jbosslogging.version>3.3.0.Final</jbosslogging.version>
102+
<javax.servlet.version>3.1.0</javax.servlet.version>
103+
<jackson.library>2.8.5</jackson.library>
71104
<junit.version>4.12</junit.version>
72105
</properties>
73-
</project>
106+
107+
<build>
108+
<pluginManagement>
109+
<plugins>
110+
<plugin>
111+
<groupId>org.apache.maven.plugins</groupId>
112+
<artifactId>maven-compiler-plugin</artifactId>
113+
<version>3.2</version>
114+
<configuration>
115+
<source>1.8</source>
116+
<target>1.8</target>
117+
</configuration>
118+
</plugin>
119+
<plugin>
120+
<groupId>org.apache.maven.plugins</groupId>
121+
<artifactId>maven-war-plugin</artifactId>
122+
<version>2.4</version>
123+
<configuration>
124+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
125+
<warName>logging-service</warName>
126+
<failOnMissingWebXml>false</failOnMissingWebXml>
127+
</configuration>
128+
</plugin>
129+
</plugins>
130+
</pluginManagement>
131+
<finalName>logging-service</finalName>
132+
</build>
133+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.config;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
8+
9+
@Configuration
10+
@EnableWebMvc
11+
@ComponentScan(basePackages = "com.baeldung")
12+
public class AppConfiguration extends WebMvcConfigurerAdapter {
13+
14+
@Override
15+
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
16+
configurer.enable();
17+
}
18+
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.config;
2+
3+
import javax.servlet.ServletContext;
4+
import javax.servlet.ServletException;
5+
6+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
7+
8+
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
9+
10+
@Override
11+
public void onStartup(ServletContext servletContext) throws ServletException {
12+
super.onStartup(servletContext);
13+
}
14+
15+
@Override
16+
protected Class<?>[] getRootConfigClasses() {
17+
return new Class[] { AppConfiguration.class };
18+
}
19+
20+
@Override
21+
protected Class<?>[] getServletConfigClasses() {
22+
return null;
23+
}
24+
25+
@Override
26+
protected String[] getServletMappings() {
27+
return new String[] { "/" };
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.ndc;
2+
3+
public class Investment {
4+
private String transactionId;
5+
private String owner;
6+
private Long amount;
7+
8+
public Investment() {
9+
}
10+
11+
public Investment(String transactionId, String owner, Long amount) {
12+
this.transactionId = transactionId;
13+
this.owner = owner;
14+
this.amount = amount;
15+
}
16+
17+
public String getTransactionId() {
18+
return transactionId;
19+
}
20+
21+
public void setTransactionId(String transactionId) {
22+
this.transactionId = transactionId;
23+
}
24+
25+
public String getOwner() {
26+
return owner;
27+
}
28+
29+
public void setOwner(String owner) {
30+
this.owner = owner;
31+
}
32+
33+
public Long getAmount() {
34+
return amount;
35+
}
36+
37+
public void setAmount(Long amount) {
38+
this.amount = amount;
39+
}
40+
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.ndc.controller;
2+
3+
import org.jboss.logging.NDC;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Qualifier;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import com.baeldung.ndc.Investment;
14+
import com.baeldung.ndc.service.InvestmentService;
15+
16+
17+
@RestController
18+
public class JBossLoggingController {
19+
@Autowired
20+
@Qualifier("JBossLoggingInvestmentService")
21+
private InvestmentService jbossLoggingBusinessService;
22+
23+
@RequestMapping(value = "/ndc/jboss-logging", method = RequestMethod.POST)
24+
public ResponseEntity<Investment> postPayment(@RequestBody Investment investment) {
25+
// Add transactionId and owner to NDC
26+
NDC.push("tx.id=" + investment.getTransactionId());
27+
NDC.push("tx.owner=" + investment.getOwner());
28+
29+
try {
30+
jbossLoggingBusinessService.transfer(investment.getAmount());
31+
} finally {
32+
// take out owner from the NDC stack
33+
NDC.pop();
34+
35+
// take out transactionId from the NDC stack
36+
NDC.pop();
37+
38+
NDC.clear();
39+
}
40+
return new ResponseEntity<Investment>(investment, HttpStatus.OK);
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.ndc.controller;
2+
3+
import org.apache.logging.log4j.ThreadContext;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Qualifier;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import com.baeldung.ndc.Investment;
14+
import com.baeldung.ndc.service.InvestmentService;
15+
16+
@RestController
17+
public class Log4J2Controller {
18+
@Autowired
19+
@Qualifier("log4j2InvestmentService")
20+
private InvestmentService log4j2BusinessService;
21+
22+
@RequestMapping(value = "/ndc/log4j2", method = RequestMethod.POST)
23+
public ResponseEntity<Investment> postPayment(@RequestBody Investment investment) {
24+
// Add transactionId and owner to NDC
25+
ThreadContext.push("tx.id=" + investment.getTransactionId());
26+
ThreadContext.push("tx.owner=" + investment.getOwner());
27+
28+
try {
29+
log4j2BusinessService.transfer(investment.getAmount());
30+
} finally {
31+
// take out owner from the NDC stack
32+
ThreadContext.pop();
33+
34+
// take out transactionId from the NDC stack
35+
ThreadContext.pop();
36+
37+
ThreadContext.clearAll();
38+
}
39+
return new ResponseEntity<Investment>(investment, HttpStatus.OK);
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.ndc.controller;
2+
3+
import org.apache.log4j.NDC;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Qualifier;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import com.baeldung.ndc.Investment;
14+
import com.baeldung.ndc.service.InvestmentService;
15+
16+
@RestController
17+
public class Log4JController {
18+
@Autowired
19+
@Qualifier("log4jInvestmentService")
20+
private InvestmentService log4jBusinessService;
21+
22+
@RequestMapping(value = "/ndc/log4j", method = RequestMethod.POST)
23+
public ResponseEntity<Investment> postPayment(@RequestBody Investment investment) {
24+
// Add transactionId and owner to NDC
25+
NDC.push("tx.id=" + investment.getTransactionId());
26+
NDC.push("tx.owner=" + investment.getOwner());
27+
28+
try {
29+
log4jBusinessService.transfer(investment.getAmount());
30+
} finally {
31+
// take out owner from the NDC stack
32+
NDC.pop();
33+
34+
// take out transactionId from the NDC stack
35+
NDC.pop();
36+
37+
NDC.remove();
38+
}
39+
return new ResponseEntity<Investment>(investment, HttpStatus.OK);
40+
}
41+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.ndc.service;
2+
3+
/**
4+
* A fake investment service.
5+
*/
6+
public interface InvestmentService {
7+
8+
/**
9+
* Sample service transferring a given amount of money.
10+
* @param amount
11+
* @return {@code true} when the transfer complete successfully, {@code false} otherwise.
12+
*/
13+
default public boolean transfer(long amount) {
14+
beforeTransfer(amount);
15+
// exchange messages with a remote system to transfer the money
16+
try {
17+
// let's pause randomly to properly simulate an actual system.
18+
Thread.sleep((long) (500 + Math.random() * 500));
19+
} catch (InterruptedException e) {
20+
// should never happen
21+
}
22+
// let's simulate both failing and successful transfers
23+
boolean outcome = Math.random() >= 0.25;
24+
afterTransfer(amount, outcome);
25+
return outcome;
26+
}
27+
28+
void beforeTransfer(long amount);
29+
30+
void afterTransfer(long amount, boolean outcome);
31+
}

0 commit comments

Comments
 (0)