Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,19 @@
package com.baeldung.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.baeldung")
public class AppConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
@@ -0,0 +1,29 @@
package com.baeldung.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfiguration.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
@@ -0,0 +1,21 @@
package com.baeldung.mdc;
import static java.lang.Math.floor;
import static java.lang.Math.random;
import java.util.UUID;
public class TransactionFactory {
private static final String[] NAMES = { "John", "Susan", "Marc", "Samantha" };
private static long nextId = 1;
public Transfer newInstance() {
String transactionId = String.valueOf(nextId++);
String owner = NAMES[(int) floor(random() * NAMES.length)];
long amount = (long) (random() * 1500 + 500);
Transfer tx = new Transfer(transactionId, owner, amount);
return tx;
}
}
@@ -0,0 +1,27 @@
package com.baeldung.mdc;
public class Transfer {
private String transactionId;
private String sender;
private Long amount;
public Transfer(String transactionId, String sender, long amount) {
this.transactionId = transactionId;
this.sender = sender;
this.amount = amount;
}
public String getSender() {
return sender;
}
public String getTransactionId() {
return transactionId;
}
public Long getAmount() {
return amount;
}
}
@@ -0,0 +1,32 @@
package com.baeldung.mdc;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.log4j.Logger;
import com.baeldung.mdc.log4j.Log4JRunnable;
import com.baeldung.mdc.log4j2.Log4J2Runnable;
import com.baeldung.mdc.slf4j.Slf4jRunnable;
public class TransferDemo {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(3);
TransactionFactory transactionFactory = new TransactionFactory();
for (int i = 0; i < 10; i++) {
Transfer tx = transactionFactory.newInstance();
// Runnable task = new Log4JRunnable(tx);
// Runnable task = new Log4J2Runnable(tx);
Runnable task = new Slf4jRunnable(tx);
executor.submit(task);
}
executor.shutdown();
}
}
@@ -0,0 +1,28 @@
package com.baeldung.mdc;
/**
* A fake transfer service simulating an actual one.
*/
public abstract class TransferService {
/** Sample service transferring a given amount of money.
* @return {@code true} when the transfer complete successfully, {@code false} otherwise. */
public boolean transfer(long amount) {
beforeTransfer(amount);
// exchange messages with a remote system to transfer the money
try {
// let's pause randomly to properly simulate an actual system.
Thread.sleep((long) (500 + Math.random() * 500));
} catch (InterruptedException e) {
// should never happen
}
// let's simulate both failing and successful transfers
boolean outcome = Math.random() >= 0.25;
afterTransfer(amount, outcome);
return outcome;
}
abstract protected void beforeTransfer(long amount);
abstract protected void afterTransfer(long amount, boolean outcome);
}
@@ -0,0 +1,26 @@
package com.baeldung.mdc.log4j;
import org.apache.log4j.MDC;
import com.baeldung.mdc.Transfer;
public class Log4JRunnable implements Runnable {
private Transfer tx;
private static Log4JTransferService log4jBusinessService = new Log4JTransferService();
public Log4JRunnable(Transfer tx) {
this.tx = tx;
}
public void run() {
MDC.put("transaction.id", tx.getTransactionId());
MDC.put("transaction.owner", tx.getSender());
log4jBusinessService.transfer(tx.getAmount());
MDC.clear();
}
}
@@ -0,0 +1,21 @@
package com.baeldung.mdc.log4j;
import org.apache.log4j.Logger;
import com.baeldung.mdc.TransferService;
public class Log4JTransferService extends TransferService {
private Logger logger = Logger.getLogger(Log4JTransferService.class);
@Override
protected void beforeTransfer(long amount) {
logger.info("Preparing to transfer " + amount + "$.");
}
@Override
protected void afterTransfer(long amount, boolean outcome) {
logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + ".");
}
}
@@ -0,0 +1,25 @@
package com.baeldung.mdc.log4j2;
import org.apache.logging.log4j.ThreadContext;
import com.baeldung.mdc.Transfer;
public class Log4J2Runnable implements Runnable {
private final Transfer tx;
private Log4J2TransferService log4j2BusinessService = new Log4J2TransferService();
public Log4J2Runnable(Transfer tx) {
this.tx = tx;
}
public void run() {
ThreadContext.put("transaction.id", tx.getTransactionId());
ThreadContext.put("transaction.owner", tx.getSender());
log4j2BusinessService.transfer(tx.getAmount());
ThreadContext.clearAll();
}
}
@@ -0,0 +1,22 @@
package com.baeldung.mdc.log4j2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.baeldung.mdc.TransferService;
public class Log4J2TransferService extends TransferService {
private static final Logger logger = LogManager.getLogger();
@Override
protected void beforeTransfer(long amount) {
logger.info("Preparing to transfer {}$.", amount);
}
@Override
protected void afterTransfer(long amount, boolean outcome) {
logger.info("Has transfer of {}$ completed successfully ? {}.", amount, outcome);
}
}
@@ -0,0 +1,22 @@
package com.baeldung.mdc.slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.mdc.TransferService;
final class Slf4TransferService extends TransferService {
private static final Logger logger = LoggerFactory.getLogger(Slf4TransferService.class);
@Override
protected void beforeTransfer(long amount) {
logger.info("Preparing to transfer {}$.", amount);
}
@Override
protected void afterTransfer(long amount, boolean outcome) {
logger.info("Has transfer of {}$ completed successfully ? {}.", amount, outcome);
}
}
@@ -0,0 +1,24 @@
package com.baeldung.mdc.slf4j;
import org.slf4j.MDC;
import com.baeldung.mdc.Transfer;
public class Slf4jRunnable implements Runnable {
private final Transfer tx;
public Slf4jRunnable(Transfer tx) {
this.tx = tx;
}
public void run() {
MDC.put("transaction.id", tx.getTransactionId());
MDC.put("transaction.owner", tx.getSender());
new Slf4TransferService().transfer(tx.getAmount());
MDC.clear();
}
}
@@ -0,0 +1,41 @@
package com.baeldung.ndc;
public class Investment {
private String transactionId;
private String owner;
private Long amount;
public Investment() {
}
public Investment(String transactionId, String owner, Long amount) {
this.transactionId = transactionId;
this.owner = owner;
this.amount = amount;
}
public String getTransactionId() {
return transactionId;
}
public void setTransactionId(String transactionId) {
this.transactionId = transactionId;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public Long getAmount() {
return amount;
}
public void setAmount(Long amount) {
this.amount = amount;
}
}
@@ -0,0 +1,41 @@
package com.baeldung.ndc.controller;
import org.jboss.logging.NDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.ndc.Investment;
import com.baeldung.ndc.service.InvestmentService;
@RestController
public class JBossLoggingController {
@Autowired
@Qualifier("JBossLoggingInvestmentService")
private InvestmentService jbossLoggingBusinessService;
@RequestMapping(value = "/ndc/jboss-logging", method = RequestMethod.POST)
public ResponseEntity<Investment> postPayment(@RequestBody Investment investment) {
// Add transactionId and owner to NDC
NDC.push("tx.id=" + investment.getTransactionId());
NDC.push("tx.owner=" + investment.getOwner());
try {
jbossLoggingBusinessService.transfer(investment.getAmount());
} finally {
// take out owner from the NDC stack
NDC.pop();
// take out transactionId from the NDC stack
NDC.pop();
NDC.clear();
}
return new ResponseEntity<Investment>(investment, HttpStatus.OK);
}
}
@@ -0,0 +1,41 @@
package com.baeldung.ndc.controller;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.ndc.Investment;
import com.baeldung.ndc.service.InvestmentService;
@RestController
public class Log4J2Controller {
@Autowired
@Qualifier("log4j2InvestmentService")
private InvestmentService log4j2BusinessService;
@RequestMapping(value = "/ndc/log4j2", method = RequestMethod.POST)
public ResponseEntity<Investment> postPayment(@RequestBody Investment investment) {
// Add transactionId and owner to NDC
ThreadContext.push("tx.id=" + investment.getTransactionId());
ThreadContext.push("tx.owner=" + investment.getOwner());
try {
log4j2BusinessService.transfer(investment.getAmount());
} finally {
// take out owner from the NDC stack
ThreadContext.pop();
// take out transactionId from the NDC stack
ThreadContext.pop();
ThreadContext.clearAll();
}
return new ResponseEntity<Investment>(investment, HttpStatus.OK);
}
}
@@ -0,0 +1,41 @@
package com.baeldung.ndc.controller;
import org.apache.log4j.NDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.ndc.Investment;
import com.baeldung.ndc.service.InvestmentService;
@RestController
public class Log4JController {
@Autowired
@Qualifier("log4jInvestmentService")
private InvestmentService log4jBusinessService;
@RequestMapping(value = "/ndc/log4j", method = RequestMethod.POST)
public ResponseEntity<Investment> postPayment(@RequestBody Investment investment) {
// Add transactionId and owner to NDC
NDC.push("tx.id=" + investment.getTransactionId());
NDC.push("tx.owner=" + investment.getOwner());
try {
log4jBusinessService.transfer(investment.getAmount());
} finally {
// take out owner from the NDC stack
NDC.pop();
// take out transactionId from the NDC stack
NDC.pop();
NDC.remove();
}
return new ResponseEntity<Investment>(investment, HttpStatus.OK);
}
}
@@ -0,0 +1,31 @@
package com.baeldung.ndc.service;
/**
* A fake investment service.
*/
public interface InvestmentService {
/**
* Sample service transferring a given amount of money.
* @param amount
* @return {@code true} when the transfer complete successfully, {@code false} otherwise.
*/
default public boolean transfer(long amount) {
beforeTransfer(amount);
// exchange messages with a remote system to transfer the money
try {
// let's pause randomly to properly simulate an actual system.
Thread.sleep((long) (500 + Math.random() * 500));
} catch (InterruptedException e) {
// should never happen
}
// let's simulate both failing and successful transfers
boolean outcome = Math.random() >= 0.25;
afterTransfer(amount, outcome);
return outcome;
}
void beforeTransfer(long amount);
void afterTransfer(long amount, boolean outcome);
}
@@ -0,0 +1,21 @@
package com.baeldung.ndc.service;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
@Qualifier("JBossLoggingInvestmentService")
public class JBossLoggingInvestmentService implements InvestmentService {
private static final Logger logger = Logger.getLogger(JBossLoggingInvestmentService.class);
@Override
public void beforeTransfer(long amount) {
logger.infov("Preparing to transfer {0}$.", amount);
}
@Override
public void afterTransfer(long amount, boolean outcome) {
logger.infov("Has transfer of {0}$ completed successfully ? {1}.", amount, outcome);
}
}
@@ -0,0 +1,22 @@
package com.baeldung.ndc.service;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
@Qualifier("log4j2InvestmentService")
public class Log4J2InvestmentService implements InvestmentService {
private static final Logger logger = LogManager.getLogger();
@Override
public void beforeTransfer(long amount) {
logger.info("Preparing to transfer {}$.", amount);
}
@Override
public void afterTransfer(long amount, boolean outcome) {
logger.info("Has transfer of {}$ completed successfully ? {}.", amount, outcome);
}
}
@@ -0,0 +1,21 @@
package com.baeldung.ndc.service;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
@Qualifier("log4jInvestmentService")
public class Log4JInvestmentService implements InvestmentService {
private Logger logger = Logger.getLogger(Log4JInvestmentService.class);
@Override
public void beforeTransfer(long amount) {
logger.info("Preparing to transfer " + amount + "$.");
}
@Override
public void afterTransfer(long amount, boolean outcome) {
logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + ".");
}
}