在java.spring中创建一个临时数据保持器。冬眠
我在一个项目中工作,该项目为移动应用提供服务,并且 我应该制作一个监控移动项目的项目。 我想做一些报告,显示这一刻有多少信息 还有其他类似的报道 但我不想直接从数据库获取监控项目中的查询。 我想在内存中创建一个临时数据保存器,并保存最后10分钟 数据(如变量或列表) 但技术上我不知道怎么做?在java.spring中创建一个临时数据保持器。冬眠,java,spring,hibernate,Java,Spring,Hibernate,我在一个项目中工作,该项目为移动应用提供服务,并且 我应该制作一个监控移动项目的项目。 我想做一些报告,显示这一刻有多少信息 还有其他类似的报道 但我不想直接从数据库获取监控项目中的查询。 我想在内存中创建一个临时数据保存器,并保存最后10分钟 数据(如变量或列表) 但技术上我不知道怎么做? 我在项目中使用Spring和Hibernate。使用map、hashMap或ConcurrentHashMap。 制作一个十分钟后更新地图的crone作业 这里有一个链接,首先,我们假设我们的程序尝试每10
我在项目中使用Spring和Hibernate。使用map、hashMap或ConcurrentHashMap。 制作一个十分钟后更新地图的crone作业
这里有一个链接,首先,我们假设我们的程序尝试每10分钟刷新一个名为SampleEntity的实体的报告。这只是一个简单的POJO
public class SampleEntity
{
// your fields and their getters and setters
}
接下来我们有一个类,我称之为SampleEntityDA,它从db查询我们的报告所需的记录在使用hibernate时,只需将结果返回为java.util.List即可(我认为这是您的主要问题之一)
最后,您需要每10分钟更新一次报告的数据持有者
您可以通过观察者模式简单地做到这一点。正如您在java中所知道的,这是由Observer类和Observable接口完成的。
因此1)ReportTimer需要扩展Observer类,2)在TimerTask中,我们需要通知侦听器;这是通过notifyobsers方法完成的
我们最后一节课的任务是刷新报告。我称之为报告生成器。这个类随时刷新报告。它还有一个java.util.List字段,其中包含db的最新数据。ReportGenerator会在其观察者(我是指ReportTimer)通知它时更新此字段
public class ReportGenerator implements Observer
{
List<SampleEntity> list = new ArrayList<SampleEntity>();
@Override
public void update(Observable o, Object arg)
{
// This method will automatically!?! executed whenever its observer notifies him.
// The arg parameter consists the new records. you just need to put it in the list field.
List<SampleEntity> list = (List<SampleEntity>) arg;
}
public void refreshReport()
{
// you can easily refresh a report with data in list field
}
public void refreshAnotherReport()
{
// you can easily refresh a report with data in list field
}
}
公共类ReportGenerator实现了Observer
{
列表=新的ArrayList();
@凌驾
公共无效更新(可观察o,对象arg)
{
//每当观察者通知他时,此方法将自动执行。
//arg参数由新记录组成。您只需将其放在列表字段中。
列表=(列表)参数;
}
公共报告()
{
//您可以使用列表字段中的数据轻松刷新报表
}
公共无效刷新其他报告()
{
//您可以使用列表字段中的数据轻松刷新报表
}
}
public class ReportTimer extends Observable
{
private Timer timer;
public static void main(String[] args)
{
// Your program starts from here
new ReportTimer().start();
}
private void start()
{
// schedule method of Timer class can execute a task repeatedly.
// This method accepts a TimerTask interface instance as its first parameter.I implement
// it as an anonymous class. TimerTask interface has a run method. Code in this method will execute repeatedly.
// Its second parameter is delay before task gets started to execute.
// And its third parameter is the interval between each execution(10min in your case)
timer = new Timer();
timer.schedule(
new TimerTask()
{
@Override
public void run()
{
notifyObservers(
new SampleEntityDA().queryFromDB() // 10 minutes passed from the last query, now its time to query from db again...
);
}
}, 100, 600000); // 600000ms = 10min
}
public void finish()
{
// call me whenever you get tired of refreshing reports
timer.cancel();
}
}
public class ReportGenerator implements Observer
{
List<SampleEntity> list = new ArrayList<SampleEntity>();
@Override
public void update(Observable o, Object arg)
{
// This method will automatically!?! executed whenever its observer notifies him.
// The arg parameter consists the new records. you just need to put it in the list field.
List<SampleEntity> list = (List<SampleEntity>) arg;
}
public void refreshReport()
{
// you can easily refresh a report with data in list field
}
public void refreshAnotherReport()
{
// you can easily refresh a report with data in list field
}
}