当前位置: 首页 > 工具软件 > Prevayler > 使用案例 >

prevayler

毛声
2023-12-01
Prevayler是一个把Java对象都保持在内存中的持久层框架,不需要数据库.并提供类似于数据库的快照存储,事务,查询等,同时还支持xml存储,远程存储等。
几个重要的接口。

Prevayler : 核心对象,使用PrevaylerFactory把pojo对象创建
如:Prevayler prevayler = PrevaylerFactory.createPrevayler(new NumberKeeper(), "demo1");
“demo1”代表存储路径



Transation, Query, TransationWithQuery
//以上3个接口定义了事务处理的接口,并通过Prevayler 执行。


通过官方的DEMO我们可以看到Prevayler框架 提供的仅仅是持久化的功能,且其设计上对数据结构设计有很大的影响,比如要求一个系统最好存在一个核心对象用来封装其他对象,从而实现存储和事务等功能,参考Demo2就知道。

[color=red]所以Prevayler仅仅适用于小型系统,且要求数据结构关联性强,针对于复杂的查询,对象存储比起数据库存储显然也不够灵活[/color]

Helloword Demo

package org.prevayler.demo.helloword;


import java.io.IOException;

import org.prevayler.Prevayler;
import org.prevayler.PrevaylerFactory;
import org.prevayler.demos.demo2.business.Account;
import org.prevayler.demos.demo2.business.AccountEntry;
import org.prevayler.demos.demo2.business.Bank;
import org.prevayler.foundation.serialization.XStreamSerializer;

import com.thoughtworks.xstream.XStream;

public class Helloworld {

Prevayler prevayler;

Prevayler xmlPrevayler;

public Helloworld() throws Exception {
prevayler = PrevaylerFactory.createPrevayler(new HelloworldBean(), "helloworld");
PrevaylerFactory factory = new PrevaylerFactory();
// 配置存储目录
factory.configurePrevalenceDirectory("helloworldXml");

// 配置存储序列化工具
factory.configureSnapshotSerializer(new XStreamSerializer() {
protected XStream createXStream() {
// 使用XStream生成Xml
XStream x = new XStream();
x.alias("helloworld", HelloworldBean.class);
return x;
}
});
// 配置持久化对象
factory.configurePrevalentSystem(new HelloworldBean());
xmlPrevayler = factory.create();
}

public void sayHello(int time) throws Exception {
prevayler.execute(new SaveHelloworld(time));
}

public void sayHelloXml(int time) throws Exception {
xmlPrevayler.execute(new SaveHelloworld(time));
}

public HelloworldBean helloBean() {
return (HelloworldBean) prevayler.prevalentSystem();
}

public HelloworldBean helloXmlBean() {
return (HelloworldBean) xmlPrevayler.prevalentSystem();
}

public void save() throws Exception {
// 保存
prevayler.takeSnapshot();
xmlPrevayler.takeSnapshot();
}

public static void main(String[] args) throws Exception {
Helloworld helloworld = new Helloworld();
System.out.println("before say , there has say for " + helloworld.helloBean().getHelloTime() + " times");
for (int i =0; i < 10; i++) {
helloworld.sayHello((int)(Math.random()*10));
}
System.out.println("after say , there has say for " + helloworld.helloBean().getHelloTime() + " times");


System.out.println("==========current save as xml===========");

System.out.println("before say xml , there has say for " + helloworld.helloXmlBean().getHelloTime() + " times");
for (int i =0; i < 10; i++) {
helloworld.sayHelloXml((int)(Math.random()*10));
}
System.out.println("after say xml , there has say for " + helloworld.helloXmlBean().getHelloTime() + " times");
helloworld.save();
}
}



package org.prevayler.demo.helloword;

import java.io.Serializable;
import java.util.Date;

public class HelloworldBean implements Serializable {

/**
*
*/
private static final long serialVersionUID = -2624803617449327171L;

private int helloTime;

private Date lastHelloTime;

public Date getLastHelloTime() {
return lastHelloTime;
}

public void setLastHelloTime(Date lastHelloTime) {
this.lastHelloTime = lastHelloTime;
}

public int getHelloTime() {
return helloTime;
}

public void setHelloTime(int helloTime) {
this.helloTime = helloTime;
}

}




package org.prevayler.demo.helloword;

import java.util.Date;

import org.prevayler.Transaction;

public class SaveHelloworld implements Transaction {

/**
*
*/
private static final long serialVersionUID = 3241456994096604069L;

private int addHelloTime;

public SaveHelloworld(int addHelloTime) {
this.addHelloTime = addHelloTime;
}

public void executeOn(Object prevalentSystem, Date executionTime) {
HelloworldBean bean = (HelloworldBean) prevalentSystem;
bean.setHelloTime(bean.getHelloTime() + addHelloTime);
bean.setLastHelloTime(executionTime);
}

}



运行helloworld

第一次输出:
before say , there has say for 0 times
after say , there has say for 69 times
==========current save as xml===========
before say xml , there has say for 0 times
after say xml , there has say for 38 times

第二次输出:
before say , there has say for 69 times
after say , there has say for 127 times
==========current save as xml===========
before say xml , there has say for 38 times
after say xml , there has say for 87 times

再看一下2种方式保存的文件
1.JavaSerializer
很多乱码。。。无法粘贴,大致就是序列化文件
2.XStreamSerializer
<helloworld>
<helloTime>87</helloTime>
<lastHelloTime>2009-10-14 10:49:36.796 CST</lastHelloTime>
</helloworld>

注: 假设不执行快照方法“helloworld.save()”,则程序运行完毕时,会自动以JavaSerializer的方式保存。。下次可以自动恢复。
 类似资料:

相关阅读

相关文章

相关问答