From 9cefd3bfc18d0706e6cd3b3595b36d9074a320f3 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 10 Apr 2013 09:03:37 +0200 Subject: o Initial import. --- src/main/java/io/trygvis/Main.java | 93 +++++++++++++++++ src/main/java/io/trygvis/MyJob.java | 33 ++++++ src/main/java/io/trygvis/model/Article.java | 57 +++++++++++ src/main/java/io/trygvis/spring/Config.java | 153 ++++++++++++++++++++++++++++ src/main/resources/applicationContext.xml | 22 ++++ src/main/resources/logback.xml | 28 +++++ 6 files changed, 386 insertions(+) create mode 100755 src/main/java/io/trygvis/Main.java create mode 100755 src/main/java/io/trygvis/MyJob.java create mode 100755 src/main/java/io/trygvis/model/Article.java create mode 100755 src/main/java/io/trygvis/spring/Config.java create mode 100755 src/main/resources/applicationContext.xml create mode 100755 src/main/resources/logback.xml (limited to 'src') diff --git a/src/main/java/io/trygvis/Main.java b/src/main/java/io/trygvis/Main.java new file mode 100755 index 0000000..c25b0d9 --- /dev/null +++ b/src/main/java/io/trygvis/Main.java @@ -0,0 +1,93 @@ +package io.trygvis; + +import io.trygvis.model.*; +import org.hibernate.dialect.*; +import org.quartz.*; +import org.quartz.impl.*; +import org.quartz.impl.triggers.*; +import org.slf4j.*; +import org.slf4j.bridge.*; +import org.springframework.beans.factory.annotation.*; +import org.springframework.context.support.*; +import org.springframework.stereotype.*; +import org.springframework.transaction.annotation.*; + +import javax.persistence.*; +import java.text.*; +import java.util.*; + +import static java.lang.System.*; + +@Component +@Transactional +public class Main { + private static final Logger log = LoggerFactory.getLogger(Main.class); + + public static void main(String[] args) + throws Exception { + SLF4JBridgeHandler.install(); + + String username = getProperty("user.name"); + setProperty("database.url", "jdbc:postgresql://localhost/" + username); + setProperty("database.username", username); + setProperty("database.password", username); + setProperty("hibernate.showSql", "true"); + setProperty("hibernate.hbm2ddl.auto", "validate"); // create + setProperty("hibernate.dialect", PostgreSQL82Dialect.class.getName()); + + log.info("Starting context"); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); + context.start(); + log.info("Started context"); + + context.getBean(Main.class).run(); + + Thread.sleep(1000 * 1000); + +// log.info("Sleeping"); +// Thread.sleep(10 * 1000); + + log.info("Stopping context"); + context.stop(); + log.info("Stopped context"); + + exit(0); + } + + @PersistenceContext + private EntityManager entityManager; + + @Autowired + private Scheduler scheduler; + + public void run() throws Exception { + log.info("Main.run"); + + JobKey jobKey = JobKey.jobKey("create-article"); + + boolean b = scheduler.checkExists(jobKey); + log.info("checkExists: {}", b); + + if(!b) { + JobDetailImpl jobDetail = new JobDetailImpl(); + jobDetail.setKey(jobKey); + jobDetail.setDurability(true); + jobDetail.setJobClass(MyJob.class); + scheduler.addJob(jobDetail, true); + } + + TriggerKey triggerKey = TriggerKey.triggerKey("my-trigger"); + boolean b1 = scheduler.checkExists(triggerKey); + log.info("checkExists: {}", b1); + if(!b1) { + CronTriggerImpl trigger = new CronTriggerImpl(); + trigger.setName("my-trigger"); + trigger.setJobKey(jobKey); + trigger.setCronExpression("0/10 * * * * ?"); + scheduler.scheduleJob(trigger); + } + +// Article article = new Article(new Date(), null, "title", "body"); +// entityManager.persist(article); + } +} diff --git a/src/main/java/io/trygvis/MyJob.java b/src/main/java/io/trygvis/MyJob.java new file mode 100755 index 0000000..600ad9b --- /dev/null +++ b/src/main/java/io/trygvis/MyJob.java @@ -0,0 +1,33 @@ +package io.trygvis; + +import io.trygvis.model.*; +import org.quartz.*; +import org.slf4j.*; + +import java.util.*; +import javax.persistence.*; + +public class MyJob implements Job { + private final Logger log = LoggerFactory.getLogger(getClass()); + + @PersistenceContext + private EntityManager entityManager; + + public void execute(JobExecutionContext context) throws JobExecutionException { + log.info("MyJob.execute: BEGIN"); + + log.info("context.getJobDetail().getKey() = {}", context.getJobDetail().getKey()); + + Date now = new Date(); + + log.info("now = {}", now); + + Article article = entityManager.find(Article.class, 1); + + System.out.println("article = " + article); + article.setUpdated(now); + entityManager.persist(article); + + log.info("MyJob.execute: END"); + } +} diff --git a/src/main/java/io/trygvis/model/Article.java b/src/main/java/io/trygvis/model/Article.java new file mode 100755 index 0000000..6383e50 --- /dev/null +++ b/src/main/java/io/trygvis/model/Article.java @@ -0,0 +1,57 @@ +package io.trygvis.model; + +import java.util.Date; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.SequenceGenerator; +import javax.persistence.Table; + +@Entity +public class Article { + @Id + @SequenceGenerator(name="id_seq", sequenceName="id_seq") + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq") + private Integer id; + private Date created; + private Date updated; + private String title; + private String body; + + @SuppressWarnings("UnusedDeclaration") + private Article() { + } + + public Article(Date created, Date updated, String title, String body) { + this.created = created; + this.updated = updated; + this.title = title; + this.body = body; + } + + public Integer getId() { + return id; + } + + public Date getCreated() { + return created; + } + + public Date getUpdated() { + return updated; + } + + public void setUpdated(Date updated) { + this.updated = updated; + } + + public String getTitle() { + return title; + } + + public String getBody() { + return body; + } +} diff --git a/src/main/java/io/trygvis/spring/Config.java b/src/main/java/io/trygvis/spring/Config.java new file mode 100755 index 0000000..b5aa1b5 --- /dev/null +++ b/src/main/java/io/trygvis/spring/Config.java @@ -0,0 +1,153 @@ +package io.trygvis.spring; + +import com.jolbox.bonecp.*; +import io.trygvis.*; +import io.trygvis.model.*; +import org.hibernate.*; +import org.hibernate.annotations.*; +import org.hibernate.cfg.*; +import org.hibernate.ejb.*; +import org.quartz.*; +import org.springframework.beans.factory.annotation.*; +import org.springframework.context.annotation.*; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.*; +import org.springframework.jdbc.datasource.*; +import org.springframework.orm.hibernate4.*; +import org.springframework.orm.jpa.*; +import org.springframework.scheduling.quartz.*; +import org.springframework.transaction.*; +import org.springframework.transaction.annotation.*; +import org.springframework.transaction.support.*; + +import java.util.*; +import javax.persistence.*; +import javax.sql.*; + +import static org.hibernate.cfg.AvailableSettings.*; +import static org.hibernate.ejb.AvailableSettings.*; + +@Configuration +@ComponentScan(basePackages = "io.trygvis") +@EnableTransactionManagement +//@EnableJpaRepositories(basePackages = "no.topi.fiken.service", repositoryImplementationPostfix = "CustomImpl") +public class Config { + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws Exception { + return new PropertySourcesPlaceholderConfigurer() {{ +// setLocation(new UrlResource("file:environment.properties")); + setProperties(System.getProperties()); + setLocalOverride(true); + }}; + } + + @Bean + public SchedulerFactoryBean quartz(DataSource dataSource) { + SchedulerFactoryBean bean = new SchedulerFactoryBean(); + bean.setDataSource(dataSource); + Properties quartzProperties = new Properties(); + quartzProperties.setProperty("org.quartz.scheduler.skipUpdateCheck", "true"); + // quartzProperties.setProperty("org.quartz.jobStore.selectWithLockSQL", "false"); + // quartzProperties.setProperty("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.PostreSQLDelegate"); + bean.setQuartzProperties(quartzProperties); + return bean; + } + +/* + @Bean(name = "my-job") + public JobDetailFactoryBean myJobDetailBean() { + JobDetailFactoryBean bean = new JobDetailFactoryBean(); + bean.setJobClass(MyJob.class); + bean.setDurability(true); + + return bean; + } + + @Bean + public CronTriggerFactoryBean myJobTrigger(JobDetail jobDetail) { + CronTriggerFactoryBean bean = new CronTriggerFactoryBean(); + bean.setName("my-trigger"); + bean.setBeanName("my-job"); + bean.setJobDetail(jobDetail); + bean.setCronExpression("0/10 * * * * ?"); + + return bean; + } +*/ + + @Bean + public DataSource dataSource(@Value("${database.url}") String jdbcUrl, + @Value("${database.username}") String username, + @Value("${database.password}") String password) { + BoneCPDataSource ds = new BoneCPDataSource(); + + ds.setLogStatementsEnabled(true); + + ds.setJdbcUrl(jdbcUrl); + ds.setUsername(username); + ds.setPassword(password); + + ds.setIdleConnectionTestPeriodInSeconds(60); + ds.setIdleMaxAgeInSeconds(240); + ds.setMaxConnectionsPerPartition(40); + ds.setMinConnectionsPerPartition(0); + ds.setPartitionCount(1); + ds.setAcquireIncrement(1); + ds.setStatementsCacheSize(1000); + ds.setReleaseHelperThreads(3); + ds.setStatisticsEnabled(true); + return new TransactionAwareDataSourceProxy(new LazyConnectionDataSourceProxy(ds)); + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, + @Value("${hibernate.hbm2ddl.auto}") String hbm2ddl, + @Value("${hibernate.showSql}") boolean showSql, + @Value("${hibernate.dialect}") String dialect) { + LocalContainerEntityManagerFactoryBean x = new LocalContainerEntityManagerFactoryBean(); + x.setDataSource(dataSource); + x.setJpaPropertyMap(createJpaMap(hbm2ddl, showSql, dialect)); + x.setPackagesToScan(Article.class.getPackage().getName()); + HibernatePersistence persistenceProvider = new HibernatePersistence(); + x.setPersistenceProvider(persistenceProvider); + return x; + } + + public static Map createJpaMap(String hbm2ddl, boolean showSql, String dialect) { + Map map = new HashMap<>(); + map.put(HBM2DDL_AUTO, hbm2ddl); + map.put(FORMAT_SQL, showSql); + map.put(SHOW_SQL, showSql); + map.put(USE_SQL_COMMENTS, showSql); + map.put(GENERATE_STATISTICS, true); + map.put(NAMING_STRATEGY, ImprovedNamingStrategy.class.getName()); + + map.put(DEFAULT_CACHE_CONCURRENCY_STRATEGY, CacheConcurrencyStrategy.READ_WRITE.toString()); + map.put(CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); +// map.put(CACHE_REGION_FACTORY, EhCacheRegionFactory.class.getName()); + map.put(USE_SECOND_LEVEL_CACHE, false); + map.put(USE_QUERY_CACHE, false); +// map.put(SHARED_CACHE_MODE, SharedCacheMode.ENABLE_SELECTIVE.toString()); + + map.put(DIALECT, dialect); + map.put("hibernate.temp.use_jdbc_metadata_defaults", "false"); + + return map; + } + + @Bean + public SessionFactory sessionFactory(LocalContainerEntityManagerFactoryBean entityManagerFactory) { + return ((HibernateEntityManagerFactory) entityManagerFactory.nativeEntityManagerFactory).getSessionFactory(); + } + + @Bean + public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { + return new JpaTransactionManager(entityManagerFactory); + } + + @Bean + public TransactionTemplate transactionTemplate(PlatformTransactionManager platformTransactionManager) { + return new TransactionTemplate(platformTransactionManager); + } +} diff --git a/src/main/resources/applicationContext.xml b/src/main/resources/applicationContext.xml new file mode 100755 index 0000000..9856a0b --- /dev/null +++ b/src/main/resources/applicationContext.xml @@ -0,0 +1,22 @@ + + + + + + diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100755 index 0000000..038a2f1 --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + -- cgit v1.2.3