?????????????????????????????????????????????????????BackupServiceExecuter????????Factory)?????????????????????????????????????????????????????BackupServiceExecuter??????BackupService????????????????????(Facade)?????????????????????????????
???????????????web??????????????BackupServiceExecuter????б???????????BackupServiceExecuter?????????????@Service??????????????????????
db.properties??db.local.properties??jpa.database?????????????????????????
/**
*??б??????????????ж??????????????????????????????????????????
* @author ???д?
*/
@Service
public class BackupServiceExecuter extends AbstractBackupService{
private BackupService backupService=null;
@Resource(name="backupFileSenderExecuter")
private BackupFileSenderExecuter backupFileSenderExecuter;
/**
* ??????????????????????????????
* @return
*/
@Override
public boolean backup() {
if(backupService==null){
backupService=SpringContextUtils.getBean(PropertyHolder.getProperty("jpa.database"));
}
boolean result = backupService.backup();
//????????????????????????????
if(result){
backupFileSenderExecuter.send(getNewestBackupFile());
}
return result;
}
/**
* ?????????????????????????????
* @param date
* @return
*/
@Override
public boolean restore(String date) {
if(backupService==null){
backupService=SpringContextUtils.getBean(PropertyHolder.getProperty("jpa.database"));
}
return backupService.restore(date);
}
}
????????????д???backupService=SpringContextUtils.getBean(PropertyHolder.getProperty("jpa.database"));
????2????????????????????????????????????????????????????????????????????????λ???????????????????????????????????????????????
????AOP??????????????????????AOP??????????????????????????淶?????????????????????????????BackupServiceExecuter????????б?????????????й???????????????????BackupService???????????????????????????????AOP??
???????AOP???????????????
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
????????????spring????????????????????????
????<aop:aspectj-autoproxy />
??????????д???????????????
/**
* ??????????????Aspect
* org.apdplat.module.system.service.backup.impl???????ж????????????????
* ?????????BackupService????backup?????????????????restore??????????????
* @author ???д?
*/
@Aspect
@Service
public class BackupLogAspect {
private static final APDPlatLogger LOG = new APDPlatLogger(BackupLogAspect.class);
private static final boolean MONITOR_BACKUP = PropertyHolder.getBooleanProperty("monitor.backup");
private BackupLog backupLog = null;
static{
if(MONITOR_BACKUP){
LOG.info("????????????");
LOG.info("Enable backup restore log"?? Locale.ENGLISH);
}else{
LOG.info("????????????");
LOG.info("Disable backup restore log"?? Locale.ENGLISH);
}
}
//???????????????
@Pointcut("execution( boolean org.apdplat.module.system.service.backup.impl.*.backup() )")
public void backup() {}
@Before("backup()")
public void beforeBackup(JoinPoint jp) {
if(MONITOR_BACKUP){
before(BackupLogType.BACKUP);
}
}
@AfterReturning(value="backup()"?? argNames="result"?? returning = "result")
public void afterBackup(JoinPoint jp?? boolean result) {
if(MONITOR_BACKUP){
after(result);
}
}
//??????????????
@Before(value="execution( boolean org.apdplat.module.system.service.backup.impl.*.restore(java.lang.String) ) && args(date)"??
argNames="date")
public void beforeRestore(JoinPoint jp?? String date) {
if(MONITOR_BACKUP){
before(BackupLogType.RESTORE);
}
}
@AfterReturning(pointcut="execution( boolean org.apdplat.module.system.service.backup.impl.*.restore(java.lang.String) )"??
returning = "result")
public void afterRestore(JoinPoint jp?? boolean result) {
if(MONITOR_BACKUP){
after(result);
}
}
private void before(String type){
LOG.info("???????????"+type+"???");
User user=UserHolder.getCurrentLoginUser();
String ip=UserHolder.getCurrentUserLoginIp();
backupLog=new BackupLog();
if(user != null){
backupLog.setUsername(user.getUsername());
}
backupLog.setLoginIP(ip);
try {
backupLog.setServerIP(InetAddress.getLocalHost().getHostAddress());
} catch (UnknownHostException e) {
LOG.error("????????????IP???"?? e);
LOG.error("Can't get server's ip address"?? e?? Locale.ENGLISH);
}
backupLog.setAppName(SystemListener.getContextPath());
backupLog.setStartTime(new Date());
backupLog.setOperatingType(type);
}
private void after(boolean result){
if(result){
backupLog.setOperatingResult(BackupLogResult.SUCCESS);
}else{
backupLog.setOperatingResult(BackupLogResult.FAIL);
}
backupLog.setEndTime(new Date());
backupLog.setProcessTime(backupLog.getEndTime().getTime()-backupLog.getStartTime().getTime());
//???????????滺????
BufferLogCollector.collect(backupLog);
LOG.info("??????");
}
}
????3????????????????????????????????浽?????????????????λ???????????????漸???????????????????????????????????棬?????????web?????????
????APDPlat????????????????????????????£?
/**
* ?????????????
* ???μ?????????????????????????????????????????
* @author ???д?
*/
public interface BackupFileSender {
public void send(File file);
}