在我的宠物项目中,我有一个长期工作,我想向用户显示有关该过程及其进展的状态.所以我正在将状态对象推送到JMS主题,从那里获取并提供给WS应用程序以将它们流式传输到有效的客户端.我在Spring AOP(即@Before,@ AfterReturn)中编写了切入点,并调用我的服务将消息发送到主题.现在我想记录服务的状态,而不是在方法开始时或在方法内部返回之后.所以我调用了服务(它注入了jmsTamplete并获取了状态对象).有没有办法最小化这些呼叫,以便我不能重复服务呼叫.这是我的sudo代码.
public class Myservice{
UserDao userDao;
LegService legservice;
ProviderDao providerDao;
....
StatusServive statusServie;
//aop will call the same service to send info to JMS topic
fetchandCalculateLeg(){
// here i called statusServie.senStatus(StatusObject);
List<Users> = userDao.fetchUserInfo();
// here i called statusServie.senStatus(StatusObject);
....
loop: #forEachUser
// here i called statusServie.senStatus(StatusObject);
someList = legservice.fecthLegInfoForEachUser();
// here i called statusServie.senStatus(StatusObject);
:endloop;
....
} }
Run Code Online (Sandbox Code Playgroud)
目前我有3个长时间运行的任务,我在每个方法类中调用相同的内容.我想尽量减少通话.
EDIT1:我也可以在调用方法上应用AOP,但这种情况下的性能是什么?如何衡量AOP性能(在应用程序启动或创建代理对象期间消耗的内存量).很抱歉问了这么多问题.
如果您使用 AOP 性能会比这更好,因为它是经过充分测试和优化的框架来执行此类操作。无论如何,您可以通过减去开始时间和结束时间来检查。
您可以使用当前的代码和 AOP 实现的代码来检查性能。这将是一项非常有趣的工作。执行如下操作来设置开始和结束时间并减去。
System.currentTimeMillis()
Run Code Online (Sandbox Code Playgroud)
Above syntax , Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds. See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC).
There are many AOP Opensource Frameworks available.
Spring AOP: AOP with Spring Framework. Advertisements. One of the key components of Spring Framework is the Aspect oriented programming (AOP) framework. Aspect Oriented Programming entails breaking down program logic into distinct parts called so-called concerns.
AspectJ: AspectJ 是 Java 编程语言的无缝面向方面扩展,兼容 Java 平台且易于学习和使用。AspectJ 支持横切关注点的干净模块化,例如:错误检查和处理、同步、上下文相关行为、性能优化、监视和日志记录、调试支持、多对象协议。
AspectWerkz: AspectWerkz 是一个动态的、轻量级的、高性能的 Java AOP 框架。AspectWerkz 既强大又简单,将帮助您轻松地将 AOP 集成到新项目和现有项目中。AspectWerkz 利用运行时字节码修改在运行时编织您的类。它挂钩并编织除引导类加载器之外的任何类加载器加载的类。它具有丰富且高度正交的连接点模型。方面、建议和介绍都是用纯 Java 编写的,您的目标类可以是常规 POJO。您可以添加、删除和重新构建建议,以及在运行时交换介绍的实现。您的方面可以使用 XML 定义文件或使用运行时属性来定义。
编辑1
Spring 还支持 AspectJ。我确实用 AspectJ 1.7.2 对其进行了测试,它没有显示内存泄漏。您可以在应用程序中复制相同的内容,方法是创建大量 bean,然后在一段时间后进行堆转储,因为垃圾收集需要一些时间,它不是即时的。
根据我的测试。性能不错,没有大量的呼叫。
MKYong 中提供了简单的示例:http ://www.mkyong.com/spring3/spring-aop-aspectj-annotation-example/
注意:您应该根据时间和空间来修改您的问题表现。而且,根据您的评论,您似乎在询问更多内存优化或管理方面的内容。另外,你的问题主题与问题不同。
| 归档时间: |
|
| 查看次数: |
821 次 |
| 最近记录: |