如何计算方法的调用次数?
How can I count the invocations of a method?
使用拦截器统计调用次数。
我使用 java ee 创建了一个应用程序,该应用程序由一个 ejb 端和一个客户端组成。现在我必须计算客户端调用方法的次数,对于每个方法,使用拦截器。
我不明白如何修改拦截器class。这是我的代码。
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class InterceptorsBean {
@AroundInvoke
public Object methodInterceptors (InvocationContext ctx) throws Exception {
System.out.println("Method invocated: " + ctx.getMethod().getName());
return ctx.proceed();
}
}
如何添加计数功能?
只需将一个字段初始化为 0,然后在每次输入该方法时将其递增。
如果该方法是静态的,则将其设为静态字段。
并将变量设为私有,并根据需要使用实例或静态方法来检索它。这是为了让其他可以调用该方法的人无法更改变量。
如果您想对多个方法执行此操作,请使用映射并将方法名称用作键来检索适当的计数器。
public class MapCounterDemo {
private Map<String, Integer> counters = new HashMap<>();
public static void main(String[] args) {
MapCounterDemo demo = new MapCounterDemo();
demo.foo();
demo.foo();
demo.foo();
demo.bar();
demo.bar();
System.out.println(demo.counters);
}
public void foo() {
update("foo");
}
public void bar() {
update("bar");
}
private void update(String method) {
counters.compute(method,
(k, v) -> v == null ? 1
: ++v);
}
}
您可以使用映射来保存方法名称和该方法的调用次数。
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import java.util.Map;
public class InterceptorsBean {
private Map<String, Integer> numberOfInvocations;
@AroundInvoke
public Object methodInterceptors (InvocationContext ctx) throws Exception {
int invocations = 1;
if(numberOfInvocations.containsKey(ctx.getMethod().getName())) {
invocations = numberOfInvocations.get(ctx.getMethod().getName()) + 1;
}
numberOfInvocations.put(ctx.getMethod().getName(), invocations);
System.out.println("Method invocated: " + ctx.getMethod().getName());
System.out.println("Number of invocations: " + invocations);
return ctx.proceed();
}
}
使用拦截器统计调用次数。
我使用 java ee 创建了一个应用程序,该应用程序由一个 ejb 端和一个客户端组成。现在我必须计算客户端调用方法的次数,对于每个方法,使用拦截器。
我不明白如何修改拦截器class。这是我的代码。
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class InterceptorsBean {
@AroundInvoke
public Object methodInterceptors (InvocationContext ctx) throws Exception {
System.out.println("Method invocated: " + ctx.getMethod().getName());
return ctx.proceed();
}
}
如何添加计数功能?
只需将一个字段初始化为 0,然后在每次输入该方法时将其递增。
如果该方法是静态的,则将其设为静态字段。
并将变量设为私有,并根据需要使用实例或静态方法来检索它。这是为了让其他可以调用该方法的人无法更改变量。
如果您想对多个方法执行此操作,请使用映射并将方法名称用作键来检索适当的计数器。
public class MapCounterDemo {
private Map<String, Integer> counters = new HashMap<>();
public static void main(String[] args) {
MapCounterDemo demo = new MapCounterDemo();
demo.foo();
demo.foo();
demo.foo();
demo.bar();
demo.bar();
System.out.println(demo.counters);
}
public void foo() {
update("foo");
}
public void bar() {
update("bar");
}
private void update(String method) {
counters.compute(method,
(k, v) -> v == null ? 1
: ++v);
}
}
您可以使用映射来保存方法名称和该方法的调用次数。
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import java.util.Map;
public class InterceptorsBean {
private Map<String, Integer> numberOfInvocations;
@AroundInvoke
public Object methodInterceptors (InvocationContext ctx) throws Exception {
int invocations = 1;
if(numberOfInvocations.containsKey(ctx.getMethod().getName())) {
invocations = numberOfInvocations.get(ctx.getMethod().getName()) + 1;
}
numberOfInvocations.put(ctx.getMethod().getName(), invocations);
System.out.println("Method invocated: " + ctx.getMethod().getName());
System.out.println("Number of invocations: " + invocations);
return ctx.proceed();
}
}