package io.trygvis; import io.trygvis.queue.*; import io.trygvis.queue.Queue; import org.hibernate.dialect.*; 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.*; import org.springframework.transaction.support.*; import java.util.*; import static java.lang.System.*; import static java.lang.Thread.*; import static org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRED; @Component 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", getProperty("jdbc.url", "jdbc:postgresql://localhost/" + username)); setProperty("database.username", username); setProperty("database.password", username); // setProperty("hibernate.showSql", "true"); setProperty("hibernate.hbm2ddl.auto", "create"); // create setProperty("hibernate.dialect", PostgreSQL82Dialect.class.getName()); log.info("Starting context"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); log.info("Started context"); try { context.getBean(Main.class).run(); log.info("Sleeping"); sleep(1000 * 1000); } catch (Exception e) { e.printStackTrace(System.out); } log.info("Stopping context"); context.stop(); log.info("Stopped context"); exit(0); } @Autowired private TransactionTemplate transactionTemplate; @Autowired private AsyncService asyncService; @Autowired @Qualifier("createArticle") private AsyncService.AsyncCallable createArticleCallable; @Autowired @Qualifier("createArticle") private AsyncService.AsyncCallable/*UpdateArticleCallable*/ updateArticleCallable; public void run() throws Exception { log.info("Main.run"); final Queue q = asyncService.registerQueue("create-queue", 10, createArticleCallable); // log.info("queue registered: ref = {}", q); // asyncService.registerQueue("update-queue", 1, updateArticeCallable); // q = asyncService.getQueue("create-queue"); final List tasks = new ArrayList<>(); transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { for (int i = 0; i < 1; i++) { tasks.add(asyncService.schedule(q)); } } }); while (true) { sleep(10000); log.info("tasks.size = {}", tasks.size()); for (Iterator iterator = tasks.iterator(); iterator.hasNext(); ) { Task task = iterator.next(); task = asyncService.update(task); log.info("task = {}", task); if (task.isDone()) { iterator.remove(); } } if (tasks.isEmpty()) { break; } } } }