Creating an MBean

To create an MBean, we can use @Managed annotations to convert any class into an MBean. The class BankTransferService transfers an amount from one account to another. We will use this example for further understanding:

@Component
@ManagedResource(objectName = "com.packt.springhighperformance.ch4.mbeans : name=BankMoneyTransferService", description = "Transfers money from one account to another")
public class BankMoneyTransferService {

private Map<String, Integer> accountMap = new HashMap<String,
Integer>();
{
accountMap.put("12345", 20000);
accountMap.put("54321", 10000);
};

@ManagedOperation(description = "Amount transfer")
@ManagedOperationParameters({
@ManagedOperationParameter(name = "sourceAccount", description =
"Transfer from account"),
@ManagedOperationParameter(name = "destinationAccount",
description = "Transfer to account"),
@ManagedOperationParameter(name = "transferAmount",
description =
"Amount to be transfer") })
public void transfer(String sourceAccount, String
destinationAccount, int transferAmount) {
if (transferAmount == 0) {
throw new IllegalArgumentException("Invalid amount");
}
int sourceAcctBalance = accountMap.get(sourceAccount);
int destinationAcctBalance = accountMap.get(destinationAccount);

if ((sourceAcctBalance - transferAmount) < 0) {
throw new IllegalArgumentException("Not enough balance.");
}
sourceAcctBalance = sourceAcctBalance - transferAmount;
destinationAcctBalance = destinationAcctBalance + transferAmount;

accountMap.put(sourceAccount, sourceAcctBalance);
accountMap.put(destinationAccount, destinationAcctBalance);
}

@ManagedOperation(description = "Check Balance")
public int checkBalance(String accountNumber) {
if (StringUtils.isEmpty(accountNumber)) {
throw new IllegalArgumentException("Enter account no.");
}
if (!accountMap.containsKey(accountNumber)) {
throw new IllegalArgumentException("Account not found.");
}
return accountMap.get(accountNumber);
}

}

In the preceding class, the @ManagedResource annotation will mark the class as MBean, and the @ManagedAttribute and @ManagedOperation annotations can be used to expose any attributes or methods. The @Component annotation will make sure that all classes annotated with @Component, @Service, or @Repository will be added to the Spring context.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset