Renaming following article review
This commit is contained in:
parent
9ae723f5ab
commit
b27a8cff06
@ -10,11 +10,11 @@ public class TransactionFactory {
|
|||||||
private static final String[] NAMES = {"John", "Susan", "Marc", "Samantha"};
|
private static final String[] NAMES = {"John", "Susan", "Marc", "Samantha"};
|
||||||
private static long nextId = 1;
|
private static long nextId = 1;
|
||||||
|
|
||||||
public Transaction newInstance() {
|
public Transfer newInstance() {
|
||||||
String transactionId = String.valueOf( nextId++ );
|
String transactionId = String.valueOf( nextId++ );
|
||||||
String owner = NAMES[ (int) floor(random()*NAMES.length) ];
|
String owner = NAMES[ (int) floor(random()*NAMES.length) ];
|
||||||
long amount = (long) (random()*1500 + 500);
|
long amount = (long) (random()*1500 + 500);
|
||||||
Transaction tx = new Transaction(transactionId, owner, amount);
|
Transfer tx = new Transfer(transactionId, owner, amount);
|
||||||
return tx;
|
return tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
package com.baeldung.mdc;
|
package com.baeldung.mdc;
|
||||||
|
|
||||||
public class Transaction {
|
public class Transfer {
|
||||||
|
|
||||||
private String transactionId;
|
private String transactionId;
|
||||||
private String owner;
|
private String sender;
|
||||||
private Long amount;
|
private Long amount;
|
||||||
|
|
||||||
public Transaction(String transactionId, String owner, long amount) {
|
public Transfer(String transactionId, String sender, long amount) {
|
||||||
this.transactionId = transactionId;
|
this.transactionId = transactionId;
|
||||||
this.owner = owner;
|
this.sender = sender;
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getOwner() {
|
public String getSender() {
|
||||||
return owner;
|
return sender;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTransactionId() {
|
public String getTransactionId() {
|
@ -9,7 +9,7 @@ import com.baeldung.mdc.log4j.Log4JRunnable;
|
|||||||
import com.baeldung.mdc.log4j2.Log4J2Runnable;
|
import com.baeldung.mdc.log4j2.Log4J2Runnable;
|
||||||
import com.baeldung.mdc.slf4j.Slf4jRunnable;
|
import com.baeldung.mdc.slf4j.Slf4jRunnable;
|
||||||
|
|
||||||
public class GenericApp {
|
public class TransferDemo {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ public class GenericApp {
|
|||||||
TransactionFactory transactionFactory = new TransactionFactory();
|
TransactionFactory transactionFactory = new TransactionFactory();
|
||||||
|
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
final Transaction tx = transactionFactory.newInstance();
|
Transfer tx = transactionFactory.newInstance();
|
||||||
|
|
||||||
//Runnable task = new Log4JRunnable(tx);
|
//Runnable task = new Log4JRunnable(tx);
|
||||||
//Runnable task = new Log4J2Runnable(tx);
|
//Runnable task = new Log4J2Runnable(tx);
|
@ -1,7 +1,7 @@
|
|||||||
package com.baeldung.mdc;
|
package com.baeldung.mdc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A fake {@link IBusinessService} simulating an actual one.
|
* A fake transfer service simulating an actual one.
|
||||||
*/
|
*/
|
||||||
public abstract class TransferService {
|
public abstract class TransferService {
|
||||||
|
|
||||||
|
@ -2,21 +2,21 @@ package com.baeldung.mdc.log4j;
|
|||||||
|
|
||||||
import org.apache.log4j.MDC;
|
import org.apache.log4j.MDC;
|
||||||
|
|
||||||
import com.baeldung.mdc.Transaction;
|
import com.baeldung.mdc.Transfer;
|
||||||
|
|
||||||
public class Log4JRunnable implements Runnable {
|
public class Log4JRunnable implements Runnable {
|
||||||
|
|
||||||
private Transaction tx;
|
private Transfer tx;
|
||||||
private static Log4JBusinessService log4jBusinessService = new Log4JBusinessService();
|
private static Log4JTransferService log4jBusinessService = new Log4JTransferService();
|
||||||
|
|
||||||
public Log4JRunnable(Transaction tx) {
|
public Log4JRunnable(Transfer tx) {
|
||||||
this.tx = tx;
|
this.tx = tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
MDC.put("transaction.id", tx.getTransactionId());
|
MDC.put("transaction.id", tx.getTransactionId());
|
||||||
MDC.put("transaction.owner", tx.getOwner());
|
MDC.put("transaction.owner", tx.getSender());
|
||||||
|
|
||||||
log4jBusinessService.transfer(tx.getAmount());
|
log4jBusinessService.transfer(tx.getAmount());
|
||||||
|
|
||||||
|
@ -4,9 +4,9 @@ import org.apache.log4j.Logger;
|
|||||||
|
|
||||||
import com.baeldung.mdc.TransferService;
|
import com.baeldung.mdc.TransferService;
|
||||||
|
|
||||||
public class Log4JBusinessService extends TransferService {
|
public class Log4JTransferService extends TransferService {
|
||||||
|
|
||||||
private Logger logger = Logger.getLogger(Log4JBusinessService.class);
|
private Logger logger = Logger.getLogger(Log4JTransferService.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void beforeTransfer(long amount) {
|
protected void beforeTransfer(long amount) {
|
@ -2,20 +2,20 @@ package com.baeldung.mdc.log4j2;
|
|||||||
|
|
||||||
import org.apache.logging.log4j.ThreadContext;
|
import org.apache.logging.log4j.ThreadContext;
|
||||||
|
|
||||||
import com.baeldung.mdc.Transaction;
|
import com.baeldung.mdc.Transfer;
|
||||||
|
|
||||||
public class Log4J2Runnable implements Runnable {
|
public class Log4J2Runnable implements Runnable {
|
||||||
private final Transaction tx;
|
private final Transfer tx;
|
||||||
private Log4J2BusinessService log4j2BusinessService = new Log4J2BusinessService();
|
private Log4J2TransferService log4j2BusinessService = new Log4J2TransferService();
|
||||||
|
|
||||||
public Log4J2Runnable(Transaction tx) {
|
public Log4J2Runnable(Transfer tx) {
|
||||||
this.tx = tx;
|
this.tx = tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
ThreadContext.put("transaction.id", tx.getTransactionId());
|
ThreadContext.put("transaction.id", tx.getTransactionId());
|
||||||
ThreadContext.put("transaction.owner", tx.getOwner());
|
ThreadContext.put("transaction.owner", tx.getSender());
|
||||||
|
|
||||||
log4j2BusinessService.transfer(tx.getAmount());
|
log4j2BusinessService.transfer(tx.getAmount());
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import org.apache.logging.log4j.Logger;
|
|||||||
|
|
||||||
import com.baeldung.mdc.TransferService;
|
import com.baeldung.mdc.TransferService;
|
||||||
|
|
||||||
public class Log4J2BusinessService extends TransferService {
|
public class Log4J2TransferService extends TransferService {
|
||||||
|
|
||||||
private static final Logger logger = LogManager.getLogger();
|
private static final Logger logger = LogManager.getLogger();
|
||||||
|
|
@ -5,9 +5,9 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import com.baeldung.mdc.TransferService;
|
import com.baeldung.mdc.TransferService;
|
||||||
|
|
||||||
final class Slf4jBusinessService extends TransferService {
|
final class Slf4TransferService extends TransferService {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(Slf4jBusinessService.class);
|
private static final Logger logger = LoggerFactory.getLogger(Slf4TransferService.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void beforeTransfer(long amount) {
|
protected void beforeTransfer(long amount) {
|
@ -2,21 +2,21 @@ package com.baeldung.mdc.slf4j;
|
|||||||
|
|
||||||
import org.slf4j.MDC;
|
import org.slf4j.MDC;
|
||||||
|
|
||||||
import com.baeldung.mdc.Transaction;
|
import com.baeldung.mdc.Transfer;
|
||||||
|
|
||||||
public class Slf4jRunnable implements Runnable {
|
public class Slf4jRunnable implements Runnable {
|
||||||
private final Transaction tx;
|
private final Transfer tx;
|
||||||
|
|
||||||
public Slf4jRunnable(Transaction tx) {
|
public Slf4jRunnable(Transfer tx) {
|
||||||
this.tx = tx;
|
this.tx = tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
MDC.put("transaction.id", tx.getTransactionId());
|
MDC.put("transaction.id", tx.getTransactionId());
|
||||||
MDC.put("transaction.owner", tx.getOwner());
|
MDC.put("transaction.owner", tx.getSender());
|
||||||
|
|
||||||
new Slf4jBusinessService().transfer(tx.getAmount());
|
new Slf4TransferService().transfer(tx.getAmount());
|
||||||
|
|
||||||
MDC.clear();
|
MDC.clear();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user