Java tutorial
/* * Copyright 2014 Ken Blair * * 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 net.kenblair.scheduler.jpa; import net.kenblair.scheduler.entity.ScheduledJob; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.MapConfiguration; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.support.CronSequenceGenerator; import org.springframework.scheduling.support.CronTrigger; import javax.persistence.*; import java.util.Date; import java.util.HashMap; import java.util.Map; import static javax.persistence.FetchType.EAGER; import static javax.persistence.TemporalType.TIMESTAMP; /** * @author kblair */ @Entity @Table(name = "JOB") @EntityListeners(AuditingEntityListener.class) public class JpaScheduledJob implements ScheduledJob { @Id @GeneratedValue private Long id; @Column(nullable = false) private String cronExpression; @Column(nullable = false) private boolean enabled; @Column(nullable = false) private String bean; @Temporal(TIMESTAMP) @Column(nullable = true) private Date lastRunDate; @ElementCollection(fetch = EAGER) @CollectionTable(name = "JOB_PROPERTY", joinColumns = @JoinColumn(name = "id", referencedColumnName = "id")) @MapKeyColumn(name = "key") @Column(name = "value") private Map<String, String> properties = new HashMap<>(); @CreatedDate @Column(nullable = false) private Date dateAdded; @LastModifiedDate @Temporal(TIMESTAMP) @Column(nullable = false) private Date dateModified; public void setCronExpression(final String cronExpression) { this.cronExpression = cronExpression; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public void setBean(String bean) { this.bean = bean; } public void setProperty(final String key, final String value) { properties.put(key, value); } public void removeProperty(final String key) { properties.remove(key); } @Override public String getId() { return id != null ? id.toString() : null; } public Long getIdLong() { return id; } @Override public Trigger getTrigger() { return new CronTrigger(cronExpression); } @Override public String getBean() { return bean; } @Override public Configuration getConfiguration() { return new MapConfiguration(properties); } @Override public Date getLastRun() { return lastRunDate; } @Override public boolean equalTo(final ScheduledJob job) { return job.getTrigger().equals(getTrigger()) && job.getBean().equals(getBean()) && job.getConfiguration().equals(getConfiguration()); } }