Java tutorial
/* * Copyright 2017 Evgeniy Khyst. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package h2backup; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.deltaspike.core.api.config.ConfigProperty; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.ejb.*; import javax.inject.Inject; import java.time.Instant; import java.util.List; import static java.util.Arrays.asList; /** * @author Evgeniy Khyst */ @Singleton @Startup @Slf4j public class BackupTimerService { private static final String LIST_DELIMITER = ","; @Resource private TimerService timerService; @Inject private BackupFacade backupFacade; @Inject @ConfigProperty(name = "h2.backup.enabled", defaultValue = "false") private Boolean enabled; @Inject @ConfigProperty(name = "h2.backup.method") private String methodName; @Inject @ConfigProperty(name = "h2.backup.scheduleExpression.year") private String year; @Inject @ConfigProperty(name = "h2.backup.scheduleExpression.month") private String month; @Inject @ConfigProperty(name = "h2.backup.scheduleExpression.dayOfMonth") private String dayOfMonth; @Inject @ConfigProperty(name = "h2.backup.scheduleExpression.dayOfWeek") private String dayOfWeek; @Inject @ConfigProperty(name = "h2.backup.scheduleExpression.hour") private String hour; @Inject @ConfigProperty(name = "h2.backup.scheduleExpression.minute") private String minute; @Inject @ConfigProperty(name = "h2.backup.scheduleExpression.second") private String second; @Inject @ConfigProperty(name = "h2.backup.scheduleExpression.timezone") private String timezone; private BackupMethod method; @Inject @ConfigProperty(name = "h2.backup.file.dir") private String directory; @Inject @ConfigProperty(name = "h2.backup.file.prefix") private String filePrefix; @Inject @ConfigProperty(name = "h2.backup.file.dateTimeFormat.pattern", defaultValue = "yyyy-MM-dd") private String dateTimeFormatPattern; @Inject @ConfigProperty(name = "h2.backup.email.from") private String from; @Inject @ConfigProperty(name = "h2.backup.email.to") private String to; private List<String> toList; @Inject @ConfigProperty(name = "h2.backup.email.subject", defaultValue = "Backup") private String subject; @Inject @ConfigProperty(name = "h2.backup.email.text") private String text; @Inject @ConfigProperty(name = "h2.backup.email.charset", defaultValue = "UTF-8") private String charset; @Inject @ConfigProperty(name = "h2.backup.maxFilesToKeep", defaultValue = "0") private Integer maxFiles; @PostConstruct public void init() { if (!enabled) { log.info("H2 database backup is disabled"); return; } if (StringUtils.isEmpty(methodName)) { log.warn("No H2 database backup methods were specified"); return; } method = BackupMethod.valueOf(methodName); if (StringUtils.isEmpty(directory)) { directory = System.getProperty("user.dir"); } toList = asList(to.split(LIST_DELIMITER)); if (text == null) { text = StringUtils.EMPTY; } String timerInfoName = getTimerInfoName(); for (Timer timer : timerService.getAllTimers()) { if (timer.getInfo() instanceof BackupTimerInfo) { BackupTimerInfo timerInfo = (BackupTimerInfo) timer.getInfo(); if (StringUtils.equals(timerInfoName, timerInfo.getName())) { log.info("H2 database backup is already scheduled: {}", timerInfo); return; } } } ScheduleExpression scheduleExpression = new ScheduleExpression(); if (StringUtils.isNoneEmpty(year)) { scheduleExpression.year(year); } if (StringUtils.isNoneEmpty(month)) { scheduleExpression.month(month); } if (StringUtils.isNoneEmpty(dayOfMonth)) { scheduleExpression.dayOfMonth(dayOfMonth); } if (StringUtils.isNoneEmpty(dayOfWeek)) { scheduleExpression.dayOfWeek(dayOfWeek); } if (StringUtils.isNoneEmpty(hour)) { scheduleExpression.hour(hour); } if (StringUtils.isNoneEmpty(minute)) { scheduleExpression.minute(minute); } if (StringUtils.isNoneEmpty(second)) { scheduleExpression.second(second); } if (StringUtils.isNoneEmpty(timezone)) { scheduleExpression.timezone(timezone); } BackupTimerInfo timerInfo = new BackupTimerInfo(timerInfoName, scheduleExpression, Instant.now()); TimerConfig timerConfig = new TimerConfig(); timerConfig.setInfo(timerInfo); timerConfig.setPersistent(true); timerService.createCalendarTimer(scheduleExpression, timerConfig); log.info("Scheduled H2 database backup: {}", timerInfo); } private String getTimerInfoName() { return getClass().getName(); } @Timeout public void backup() { backupFacade.backup(method, directory, filePrefix, dateTimeFormatPattern, from, toList, subject, text, charset, maxFiles); } }