我的服务会在每次请求时将大量数据(从txt文件)加载到内存中。 但是,我想将数据保存在内存中。 因为它是从相同的txt文件中读取的。
public class pirTMain {
public String[] RUN_pirT(...){
...
//this object will read txt files to initialize
ELC elc = new ELC(elcFolder.getPath());
//use elc to initialize a graph
pirT.initGraph(userID, nodeFile.getPath(), userScore, elc, true, begin, target);
//Use graph to search paths
itinerary = pirT.search(userID, TopK, begin, beginWithTime, target, targetWithTime);
...
我读过Axis2文件。 它说我可以将服务范围改为“应用程序”。 但我仍然不知道该怎么做,因为我使用eclipse插件生成一个Web服务* .arr。 谁能建议我如何将elc对象与另一个服务分开? 然后,我的pirTMain类可以使用它。
pirTMain是'请求'。
elc是'申请'。
非常感谢。
答案 0 :(得分:0)
有很多不同的方法可以实现这一点,我想到的最简单的方法是创建一个对读取行的静态引用,以便它在同一个虚拟机中的所有线程之间共享:
@WebService
public MyServiceClass {
private static String[] readLines = null;
private static synchronized getLines(){
if (readLines == null)
readLines = ....;
return readLines;
}
public int getNumberOfLines(){
return MyServiceClass.getLines().length;
}
public String getLine(int position){
return MyServiceClass.getLines()[position];
}
...
}
可能不是“最干净”的方式,但它有效并且很容易做到。如果您愿意,还可以使用更标准的“单例模式”包装登录。请记住,getLines应该被同步为线程安全但如果你遇到瓶颈删除synchronized关键字,你可能会在第一次调用时获得无用的读取,但会更快。