How to Monitor an existing Java class with JMX? - java

I have an existing Java class as follows and I want to monitor number of method invocations for each method in this class using JMX. How do I do it? I tried google but I can't see the big picture on how the whole thing is connected. It would be great if I can see see some code examples
Public class RPCServer {
public void storeSchema() { // want to count number of method invocations
System.out.println("storeSchema");
}
public void getSchema() { // want to count number of method invocations
System.out.println("getSchema");
}
public void storeRow() { // want to count number of method invocations
System.out.println("storeRow");
}
public void getRow() { //want to count number of method invocations
System.out.println("getRow");
}
}

I you want to see how many time some methods are executed through JMX, I propose this solution
First you need an interface for your class. Only the methods of this interface are visible for JMX:
public interface RPCServerInterface {
int countMethodInvocation(String method);
}
Then in the class you store how many time each function is call.
public class RPCServer implements RPCServerInterface{
private int row;
private Map<String,Integer> countByMethod = new HashMap<String,Integer>();
// +1 to the number of time of execution of this method
private void sumMethodInvocation(String method) {
if ( countByMethod.containsKey(method) ) {
int n = countByMethod.get(method);
countByMethod.put(method, n+1);
} else {
countByMethod.put(method,1);
}
}
// how many time the method has been invoked
#Override
public int countMethodInvocation(String method){
return countByMethod.containsKey(method)?countByMethod.get(method):0;
}
public void setRow(int i) {
// register each time is executed
this.sumMethodInvocation("setRow");
this.row = i;
}
public int getRow() {
// register each time is executed
this.sumMethodInvocation("getRow");
return row;
}
}}
}
Then you have to register your Bean:
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
RPCServer rpcServer = new RPCServer();
ObjectName objectName = new ObjectName("org.foo.RPCServer.jmx:type=RPCServerInterface");
StandardMBean standardMBean = new StandardMBean(rpcServer,RPCServerInterface.class);
mBeanServer.registerMBean(standardMBean, objectName);
The path org.foo.RPCServer.jmx is arbitrary.
Then your run jconsole and you find the process you are running.
Then you can run the command countMethodInvocation and you can get the number of execution time.
Like this:
This tutorial can be useful:
what-is-jmx-mbean-jconsole-tutorial

Related

Is it possible to get StackOverflowError without recursion?

I have a task to get "StackOverflowError" in java without using -Xss and recursion. I really don't have ideas... Only some nonsense like generating huge java class at runtime, compile it and invoke...
Java stores primitive types on the stack. Objects created in local scope are allocated on the heap, with the reference to them on the stack.
You can overflow the stack without recursion by allocating too many primitive types in method scope. With normal stack size settings, you would have to allocate an excessive number of variables to overflow.
Here is the implementation of Eric J. idea of generating excessive number of local variables using javassist library:
class SoeNonRecursive {
static final String generatedMethodName = "holderForVariablesMethod";
#SneakyThrows
Class<?> createClassWithLotsOfLocalVars(String generatedClassName, final int numberOfLocalVarsToGenerate) {
ClassPool pool = ClassPool.getDefault();
CtClass generatedClass = pool.makeClass(generatedClassName);
CtMethod generatedMethod = CtNewMethod.make(getMethodBody(numberOfLocalVarsToGenerate), generatedClass);
generatedClass.addMethod(generatedMethod);
return generatedClass.toClass();
}
private String getMethodBody(final int numberOfLocalVarsToGenerate) {
StringBuilder methodBody = new StringBuilder("public static long ")
.append(generatedMethodName).append("() {")
.append(System.lineSeparator());
StringBuilder antiDeadCodeEliminationString = new StringBuilder("long result = i0");
long i = 0;
while (i < numberOfLocalVarsToGenerate) {
methodBody.append(" long i").append(i)
.append(" = ").append(i).append(";")
.append(System.lineSeparator());
antiDeadCodeEliminationString.append("+").append("i").append(i);
i++;
}
antiDeadCodeEliminationString.append(";");
methodBody.append(" ").append(antiDeadCodeEliminationString)
.append(System.lineSeparator())
.append(" return result;")
.append(System.lineSeparator())
.append("}");
return methodBody.toString();
}
}
and tests:
class SoeNonRecursiveTest {
private final SoeNonRecursive soeNonRecursive = new SoeNonRecursive();
//Should be different for every case, or once generated class become
//"frozen" for javassist: http://www.javassist.org/tutorial/tutorial.html#read
private String generatedClassName;
#Test
void stackOverflowWithoutRecursion() {
generatedClassName = "Soe1";
final int numberOfLocalVarsToGenerate = 6000;
assertThrows(StackOverflowError.class, () -> soeNonRecursive
.createClassWithLotsOfLocalVars(generatedClassName, numberOfLocalVarsToGenerate));
}
#SneakyThrows
#Test
void methodGeneratedCorrectly() {
generatedClassName = "Soe2";
final int numberOfLocalVarsToGenerate = 6;
Class<?> generated = soeNonRecursive.createClassWithLotsOfLocalVars(generatedClassName, numberOfLocalVarsToGenerate);
//Arithmetic progression
long expected = Math.round((numberOfLocalVarsToGenerate - 1.0)/2 * numberOfLocalVarsToGenerate);
long actual = (long) generated.getDeclaredMethod(generatedMethodName).invoke(generated);
assertEquals(expected, actual);
}
}
EDIT:
The answer is incorrect, because it is one type of recursion. It is called indirect recursion https://en.wikipedia.org/wiki/Recursion_(computer_science)#Indirect_recursion.
I think the simplest way to do this without recursion is the following:
import java.util.LinkedList;
import java.util.List;
interface Handler {
void handle(Chain chain);
}
interface Chain {
void process();
}
class FirstHandler implements Handler {
#Override
public void handle(Chain chain) {
System.out.println("first handler");
chain.process();
}
}
class SecondHandler implements Handler {
#Override
public void handle(Chain chain) {
System.out.println("second handler");
chain.process();
}
}
class Runner implements Chain {
private List<Handler> handlers;
private int size = 5000; // change this parameter to avoid stackoverflowerror
private int n = 0;
public static void main(String[] args) {
Runner runner = new Runner();
runner.setHandlers();
runner.process();
}
private void setHandlers() {
handlers = new LinkedList<>();
int i = 0;
while (i < size) {
// there can be different implementations of handler interface
handlers.add(new FirstHandler());
handlers.add(new SecondHandler());
i += 2;
}
}
public void process() {
if (n < size) {
Handler handler = handlers.get(n++);
handler.handle(this);
}
}
}
At first glance this example looks a little crazy, but it's not as unrealistic as it seems.
The main idea of this approach is the chain of responsibility pattern. You can reproduce this exception in real life by implementing chain of responsibility pattern. For instance, you have some objects and every object after doing some logic call the next object in chain and pass the results of his job to the next one.
You can see this in java filter (javax.servlet.Filter).
I don't know detailed mechanism of working this class, but it calls the next filter in chain using doFilter method and after all filters/servlets processing request, it continue working in the same method below doFilter.
In other words it intercepts request/response before servlets and before sending response to a client.It is dangerous piece of code because all called methods are in the same stack at the same thread. Thus, it may initiate stackoverflow exception if the chain is too big or you call doFilter method on deep level that also provide the same situation. Perhaps, during debugging you might see chain of calls
in one thread and it potentially can be the cause of stackoverflowerror.
Also you can take chain of responsibility pattern example from links below and add collection of elements instead of several and you also will get stackoverflowerror.
Links with the pattern:
https://www.journaldev.com/1617/chain-of-responsibility-design-pattern-in-java
https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern
I hope it was helpful for you.
Since the question is very interesting, I have tried to simplify the answer of hide :
public class Stackoverflow {
static class Handler {
void handle(Chain chain){
chain.process();
System.out.println("yeah");
}
}
static class Chain {
private List<Handler> handlers = new ArrayList<>();
private int n = 0;
private void setHandlers(int count) {
int i = 0;
while (i++ < count) {
handlers.add(new Handler());
}
}
public void process() {
if (n < handlers.size()) {
Handler handler = handlers.get(n++);
handler.handle(this);
}
}
}
public static void main(String[] args) {
Chain chain = new Chain();
chain.setHandlers(10000);
chain.process();
}
}
It's important to note that if stackoverflow occurs, the string "yeah" will never be output.
Of course we can do it :) . No recursion at all!
public static void main(String[] args) {
throw new StackOverflowError();
}
Looking at this answer below, not sure if this works for Java, but sounds like you can declare an array of pointers? Might be able to achieve Eric J's idea without requiring a generator.
Is it on the Stack or Heap?
int* x[LARGENUMBER]; // The addresses are held on the stack
int i; // On the stack
for(i = 0; i < LARGENUMBER; ++i)
x[i] = malloc(sizeof(int)*10); // Allocates memory on the heap

Java 8 - usage of Consumer's andThen

I have below POC to use Java 8 feature.
I want to update DB after accept method. Is it good to go with andThen()? When is this method called? Who calls it?
What is the basic use of andThen() method? Looking at the docs was confusing.
public class StockTest {
public static void main(String[] args) {
List<Trader> traders = new ArrayList<>();
Random random = new Random();
// Initializing trading a/c's.
for (int i = 0; i < 10; i++) {
Trader trader = new Trader((random.nextInt(100) + 1) * 3);
traders.add(trader);
}
// Display Trade accounts.
System.out.println("Before Bonus, Units are:");
for (Trader trader : traders) {
System.out.print(trader.getUnits() + "\t");
}
// Add bonus to each trader.
traders.forEach(new Consumer<Trader>() {
#Override
public void accept(Trader trader) {
trader.updateBonus(2);
}
#Override
public Consumer<Trader> andThen(Consumer<? super Trader> after)
{
System.out.println("In andThen");
return Consumer.super.andThen(after);
}
});
// Display Trade accounts after bonus applied..
System.out.println("\nAfter bonus:");
for (Trader trader : traders) {
System.out.print(trader.getUnits() + "\t");
}
}
}
class Trader {
private int units;
public Trader(int initialUnits) {
this.units = initialUnits;
}
public int getUnits() {
return units;
}
public void setUnits(int units) {
this.units = units;
}
public void updateBonus(int bonusUnits) {
this.units = this.units * bonusUnits;
}
}
Please help with some example or use cases to utilize this method
In short andThen is used to chain consumers, so the input will go to first and second consumer, lke below:
Consumer<Trader> consumer1 = new Consumer<Trader>() {
#Override
public void accept(Trader trader) {
trader.updateBonus(2);
}
};
Consumer<Trader> consumer2 = new Consumer<Trader>() {
#Override
public void accept(Trader trader) {
// do something
}
};
// Add bonus to each trader.
traders.forEach(consumer1.andThen(consumer2));
So here the Trader will be passed to consumer1, then to consumer2 and so on.
You don't have to implement this method, or override it. When it comes to Consumers, implement only the accept.
andThen method is a helper tool to join consumers. Instead of passing the input to all of them in a loop.
You use andThen when you want to chain the logic of two Consumers. consumer1.andThen(consumer2) first calls the accept method of consumer1 and then calls the accept method of consumer2.
Overriding the default implementation of andThen makes little sense and prevents you from using lambda expressions/method references.
andThen can be used to chain two Consumers:
traders.forEach(((Consumer<Trader>)(trader -> trader.updateBonus(2))).andThen(trader -> System.out.println("some more processing")));
Of course, in this example you can simply put the logic of the two Consumers in a single Consumer:
traders.forEach(trader -> {trader.updateBonus(2);
System.out.println("some more processing");});
It makes more sense to use andThen when you are chaining two existing Consumers:
Consumer<Trader> traderConsumer1 = trader -> trader.updateBonus(2);
Consumer<Trader> traderConsumer2 = trader -> System.out.println(trader);
traders.forEach(traderConsumer1.andThen(traderConsumer2));

Mockito : Testing void methods gives InvalidUseOfMatchersException

I'm having problems with two void methods. In encouragedVenturesScoring I've followed this answer mocking an arraylist that will be looped in a for loop and haven't mocked the list, but passed a real list and added mocked objects.
Mockito gives me an InvalidUseOfMatchersException on this line
verify(effectList.get(Mockito.anyInt())).execute(playerHandler);
There are lots of questions on SO on this exception , and I think it's because of anyInt(). Anyway I changed it to
verify(effectList.get(0)).execute(playerHandler);
And now it's saying Wanted but not invoked effect.execute(playerHandler)
Actually there were zero interactions with this mock
Is it because I put doNothing ?
doNothing().when(effect).execute(playerHandler);
In my second method militaryStrengthScoring() method is there a way to skip the first chunk of code and just test the if..else condition? What would be the best approach to test this method?
Thank you for your time.
This is the class to be tested
public class EndGameScoringBaseController implements EndGameScoringHandler {
private static final int[] TERRITORIES_REWARD = {0,0,1,4,10,20};
private static final int[] CHARACTERS_REWARD = {1,3,6,10,15,21};
private static final int RESOURCES_RATE = 5;
private static final int FIRST_MILITARY_REWARD = 5;
private static final int SECOND_MILITARY_REWARD = 2;
private PlayerHandler player;
public EndGameScoringBaseController(PlayerHandler player) {
super();
this.player = player;
}
#Override
public void encouragedVenturesScoring() {
for (DevelopmentCard card : player.getPlayer().getPersonalBoard().getVentures()) {
for (Effect e : card.getPermanentEffects())
e.execute(player);
}
}
#Override
public void militaryStrengthScoring(GameController game) {
Set<Integer> points = new HashSet<>();
int myPoints = this.player.getPointsHandler().getMilitaryPoints();
for (PlayerHandler p: game.getPlayers()) {
points.add(p.getPointsHandler().getMilitaryPoints());
}
int[] rank = new int[points.size()];
int j = 0;
for (Integer i : points) {
rank[j] = i;
j++;
}
Arrays.sort(rank);
if (rank[rank.length-1] == myPoints) {
player.getPointsHandler().winMilitaryPoints(FIRST_MILITARY_REWARD);
}
else if (rank[rank.length-2] == myPoints) {
player.getPointsHandler().winVictoryPoints(SECOND_MILITARY_REWARD);
}
}
Tested method for encouragedVenturesScoring
#Test
public void encouragedVenturesScoringTest() {
//given
List<DevelopmentCard> ventureList;
ventureList = Arrays.asList(developmentCard, developmentCard);
when(playerHandler.getPlayer().getPersonalBoard().getVentures()).thenReturn(ventureList);
List<Effect> effectList;
effectList = Arrays.asList(effect, effect);
when(developmentCard.getPermanentEffects()).thenReturn(effectList);
doNothing().when(effect).execute(playerHandler);
//when
endgameController.encouragedVenturesScoring();
//then
verify(effectList.get(Mockito.anyInt())).execute(playerHandler);
}
Incomplete tested method for militaryStrengthScoring
#Test
public void militaryStrengthScoringTest() {
//given
when(playerHandler.getPointsHandler().getMilitaryPoints()).thenReturn(4);
doNothing().when(playerHandler.getPointsHandler()).winMilitaryPoints(FIRST_MILITARY_REWARD);
//when
endgameController.militaryStrengthScoring(gameController);
//then
/../
}
You're right that this is the problem:
verify(effectList.get(Mockito.anyInt())).execute(playerHandler);
Mockito only allows for calls like any() and anyInt() to stand in for parameters to the mock themselves, due to the internal implementation of matchers.
/* OK */ when(yourMock.yourMethod(anyInt())).thenReturn(42);
/* BAD */ when(yourList.get(anyInt()).yourMethod(0)).thenReturn(42);
/* OK */ verify(yourMock).yourMethod(anyInt());
/* BAD */ verify(yourList.get(anyInt())).yourMethod(0);
The failure with get(0) is likely an actual failure, and may be related to the fact that your encouragedVenturesScoringTest is actually not calling encouragedVenturesScoring, it's calling influencedCharactersScoring. If this continues to give you trouble after fixing that error, in ways related to Mockito, please edit your question.
You can only verify mock objects created by Mockito.
But effectList is a "real" list. Therefore Mockito knows nothing about that object. Thus any attempt to verify that list must fail.
If you want to verify that object - then you have to mock it!
Of course, this means that you have specify all calls that will go to the mocked list.

Getting number of calls to a mock

Assume I want to test code like this:
class ClassToTest
// UsedClass1 contains a method UsedClass2 thisMethod() {}
UsedClass1 foo;
void aMethod()
{
int max = new Random().nextInt(100);
for(i = 0; i < max; i++)
{
foo.thisMethod().thatMethod();
}
}
}
If I have a test like this:
ClassToTest test;
UsedClass1 uc1;
UsedClass2 uc2;
#Test
public void thingToTest() {
test = new ClassToTest();
uc1 = mock(UsedClass1.class);
uc2 = mock(UsedClass2.class);
when(uc1.thisMethod()).thenReturn(uc2);
when(uc2.thatMethod()).thenReturn(true);
test.aMethod();
// I would like to do this
verifyEquals(callsTo(uc1.thisMethod()), callsTo(uc2.thatMethod()));
}
How can I get the number of calls to uc1.thisMethod() and uc2.thatMethod() so I can check they were both called the same number of times?
You can do something like this:
YourService serviceMock = Mockito.mock(YourService.class);
// code using YourService
// details of all invocations including methods and arguments
Collection<Invocation> invocations = Mockito.mockingDetails(serviceMock).getInvocations();
// just a number of calls of any mock's methods
int numberOfCalls = invocations.size();
If you want only the invocations of certain method/param combination you, you can do so with
int specificMethodCall = Mockito.mockingDetails(serviceMock.myMethod(myParam)).getInvocations()
You could stub your methods, and increment a counter, like this:
final AtomicInteger countCall1 = new AtomicInteger();
Mockito.doAnswer(new Answer<UsedClass2>() {
#Override
public UsedClass2 answer(InvocationOnMock invocation) throws Throwable {
countCall1.incrementAndGet();
return uc2;
}
}).when(uc1).thisMethod();
If you know the number of times a method is suppoed to be called you can use the times() method of Mockito
//for example if had to be called 3 times
verify(uc1, times(3)).thisMethod();
verify(uc2, times(3)).thatMethod();
However, I now see that you call the method a random number of times, so this probably isn't the best answer unless you stub out the random number generator to always return an expected value.
You can use a custom VerificationMode to count the invocations, here you go:
public class InvocationCounter {
public static <T> T countInvocations(T mock, AtomicInteger count) {
return Mockito.verify(mock, new Counter(count));
}
private InvocationCounter(){}
private static class Counter implements VerificationInOrderMode, VerificationMode {
private final AtomicInteger count;
private Counter(AtomicInteger count) {
this.count = count;
}
public void verify(VerificationData data) {
count.set(data.getAllInvocations().size());
}
public void verifyInOrder(VerificationDataInOrder data) {
count.set(data.getAllInvocations().size());
}
#Override
public VerificationMode description(String description) {
return VerificationModeFactory.description(this, description);
}
}
}
And then use it like this (works also with void return types):
#Mock
private Function<String, Integer> callable;
AtomicInteger count= new AtomicInteger(); //here is the actual invocation count stored
countInvocations(callable,count).apply( anyString());
assertThat(count.get(),is(2));

count how many times is method called

How can I count how many times is dependency.startService(); method call? Different Services are calling this method and I don't want to get how many time did everyone call that method but single Service. I should get this output :
My name is Service B and I'm depending on Service A
My name is Service C and I'm depending on Service A
My name is Service D and I'm depending on Service B
***Service Service C lets start!***
1
***Service Service D lets start!***
2
Actually this number should mean how many services these two are depending on.
Do you have any ideas on how can I do this?
I have tried and I can only get a global number of calling that method which is 3.
Here is my code:
ManagerService.java
import java.util.*;
import java.util.concurrent.CountDownLatch;
public class ManagerService
{
public static void main(String[] args) throws InterruptedException
{
//Creating Services
Service serviceA = new Service("Service A", "Thread A");
Service serviceB = new Service("Service B", "Thread B");
Service serviceC = new Service("Service C", "Thread C");
Service serviceD = new Service("Service D", "Thread D");
serviceB.dependesOn(serviceA);
serviceC.dependesOn(serviceA);
serviceD.dependesOn(serviceB);
System.out.println();
System.out.println("***Service " + serviceC.serviceName +" lets start!***");
serviceC.startService();
System.out.println();
System.out.println("***Service " + serviceD.serviceName +" lets start!***");
serviceD.startService();
}
}
and
Service.java
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
public class Service
{
public String serviceName;
public String threadName;
private boolean onOrOff = false;
public List <Service> dependentServicesOn = new ArrayList <Service>();
public CountDownLatch startSignal;
private Integer counter = 0;
public Service(String service_name, String thread_name)
{
this.serviceName = service_name;
this.threadName = thread_name;
}
public void dependesOn(Service s) throws InterruptedException
{
System.out.println("My name is " + serviceName +" and i'm depending on " + s.serviceName);
dependentServicesOn.add(s);
}
public Service startService() throws InterruptedException
{
for(Service dependency : dependentServicesOn) {
if(!dependency.isStarted()) {
dependency.startService();
}
}
startSignal = new CountDownLatch(1);
// new Thread(new CreateThread(this,startSignal)).start();
startSignal.countDown();
return null;
}
public boolean isStarted()
{
return onOrOff;
}
public void setStarted()
{
onOrOff = true;
}
}
You can set a variable count. For each method call increase the value by one. If you want to access the variable from outside of the class you can set it public static.
So something like this can be done
public static long count = 0;
public Service startService() throws InterruptedException
{
count++;
// method tasks
}
When you need to check you can check the count variable.
Each Service already has a List that stores the "parent" (services on which it is dependent). So, the size of that list is the count of direct parents. Since you also want to go further and find indirect dependencies, you can do it by "asking" each parent service how many services it depends upon.
The code would look something like this:
public int getCountOfDependencies()
{
int theCount = 0;
for (Service nxtService : dependentServicesOn)
{
theCount++; //Add one for the "direct parent"
theCount += nxtService.getCountOfDependencies(); //Also add grand-parents etc.
}
return theCount;
}
Warning! This will not work if a service can be dependent on another service via two or more "paths". For instance, consider this scenario:
serviceB.dependesOn(serviceA);
serviceC.dependesOn(serviceA);
serviceD.dependesOn(serviceB);
serviceD.dependesOn(serviceC);
Now, A is a "grand-parent" of D in two ways. So, if you call getCountOfDependencies() on D, it will count the direct parents (B and C) and will ask each of those to report their dependencies. They will each report 1, and thus A will be double-counted.
So, if you can have that type of situation, you will have to modify the approach.
If I understand the question correctly, the following should work.
Add an ArrayList<String> dependentServices as a class member.
Change startService() to startService(String callerName).
When starting a service, for example serviceA, call serviceA.startService(serviceName);
In your startService(...) method add the following:
boolean dependent = false;
for(int i = 0; i < dependentServices.size(); i++) {
if(dependentServices.get(i).equals(callerName) {
dependent = true;
}
}
if(!dependent) {
dependentServices.add(callerName);
}
In each service, the ArrayList dependentServices will now keep a list of the names of the services which have called it. The size of the ArrayList will tell you how many services there are.
Simply have a StaticCounter class and use that for identifying counts by incrementing the values.
StaticCounter.java
public class StaticCounter {
static int COUNTER;
}
JTry.java
public class JTry {
public static void main(String[] args) {
for(int i = 0; i<5; i++) {
System.err.println("Passing checkpoint " + StaticCounter.COUNTER++);
}
}

Categories