Guest User

Untitled

a guest
Oct 27th, 2017
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.37 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package jobs;
  6.  
  7. import com.simbat.iem.cli.Contact;
  8. import com.simbat.iem.cli.io.ContactInput;
  9. import com.simbat.iem.cli.io.JSonContactStream;
  10. import com.simbat.iem.cli.io.XmlContactStream;
  11. import java.io.EOFException;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.text.DateFormat;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Calendar;
  17. import java.util.Date;
  18. import java.util.List;
  19. import models.Project;
  20. import models.Subscriber;
  21. import models.SubscriberProject;
  22. import play.Logger;
  23. import play.Play;
  24. import play.jobs.Job;
  25. import play.jobs.On;
  26. import play.libs.WS;
  27.  
  28. /**
  29.  * A job to request emails from projects
  30.  * Imports emails yesterday
  31.  * Warning: it uses same username/password to request emails for every project!
  32.  * @author lauri
  33.  */
  34. @On("0 0 5 * * ?")
  35. public class SubscribersImportJob extends Job {
  36.  
  37.     public static final String CONF_USERNAME = "office.iem.import.username";
  38.     public static final String CONF_PASSWORD = "office.iem.import.password";
  39.  
  40.     @Override
  41.     public void doJob() throws Exception {
  42.         List<Project> projects = Project.findActive();
  43.         for (Project project : projects) {
  44.             importEmails(project);
  45.         }
  46.         (new SubscribersExportJob()).in(20);
  47.     }
  48.  
  49.     private void importEmails(Project project) {
  50.         if (project.emailExportUrl == null) {
  51.             Logger.debug("Project {0} is not configured for subscriber collection", project.name);
  52.         } else {
  53.             //Date project.emailExportedDate;
  54.             Date date = dateOrYesterday(project.emailExportedDate);
  55.             WS.HttpResponse response = WS.url(project.emailExportUrl, datestamp(date)).
  56.                     authenticate(
  57.                     Play.configuration.getProperty(CONF_USERNAME),
  58.                     Play.configuration.getProperty(CONF_PASSWORD)).
  59.                     get();
  60.  
  61.             InputStream is = response.getStream();
  62.             ContactInput in = response.getContentType().contains("xml") ? new XmlContactStream(is) : new JSonContactStream(is);
  63.  
  64.             try {
  65.                 for (Contact contact = in.readContact(); contact != null; contact = in.readContact()) {
  66.                     Subscriber subscriber = Subscriber.findByEmail(contact.getEmail());
  67.                     if (subscriber == null) {
  68.                         subscriber = Subscriber.create(contact, project);
  69.                         subscriber.save();
  70.                     } else {
  71.                         if (contact.getName() != null) {
  72.                             subscriber.name = contact.getName();
  73.                         }
  74.                         if (contact.getCountry() != null) {
  75.                             subscriber.country = contact.getCountry();
  76.                         }
  77.                         if (contact.getCity() != null) {
  78.                             subscriber.city = contact.getCity();
  79.                         }
  80.                         //@todo: do we need a check here?
  81.                         subscriber.projects.add(SubscriberProject.create(subscriber, project));
  82.                         subscriber.needSync = true;
  83.                         subscriber.save();
  84.                     }
  85.                 }
  86.             } catch (EOFException ex) {
  87.                 // ok, finished
  88.             } catch (IOException ex) {
  89.                 Logger.warn(ex, "A I/O error during email import");
  90.             } finally {
  91.                 project.emailExportedDate = date;
  92.                 project.save();
  93.             }
  94.         }
  95.     }
  96.  
  97.     private Date dateOrYesterday(Date date) {
  98.         if (date == null) {
  99.             return null;
  100.         }
  101.         Calendar cal = Calendar.getInstance();
  102.         cal.setTime(new Date());
  103.         cal.add(Calendar.DATE, -1);
  104.  
  105.         Date yesterday = cal.getTime();
  106.         return (date.compareTo(yesterday) > 0) ? yesterday : date;
  107.     }
  108.  
  109.     private String datestamp(Date date) {
  110.         DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  111.         if (date == null) {
  112.             return "2011-01-01";
  113.         }
  114.         return df.format(date);
  115.     }
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. /*
  124.  
  125. 2011-09-08 14:10:45,413 ERROR Batch entry 0 insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values ('Amsterdam', 'NL', 'foo@bar.com', 'Guy with cashout', '1', '1') was aborted.  Call getNextException to see the cause.
  126. 2011-09-08 14:10:45,413 ERROR ERROR: duplicate key value violates unique constraint "Subscriber_pkey"
  127.   Подробности: Key (id)=(1) already exists.
  128. 2011-09-08 14:10:45,413 ERROR Could not synchronize database state with session
  129. org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
  130.     at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
  131.     at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
  132.     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
  133.     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
  134.     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
  135.     at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
  136.     at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2411)
  137.     at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2874)
  138.     at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
  139.     at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
  140.     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
  141.     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
  142.     at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:345)
  143.     at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
  144.     at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
  145.     at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795)
  146.     at play.db.jpa.JPABase._save(JPABase.java:47)
  147.     at play.db.jpa.GenericModel.save(GenericModel.java:184)
  148.     at jobs.SubscribersImportJob.importEmails(SubscribersImportJob.java:69)
  149.     at jobs.SubscribersImportJob.doJob(SubscribersImportJob.java:44)
  150.     at play.jobs.Job.doJobWithResult(Job.java:50)
  151.     at play.jobs.Job.call(Job.java:146)
  152.     at play.jobs.Job$2.call(Job.java:94)
  153.     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
  154.     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  155.     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
  156.     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
  157.     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  158.     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  159.     at java.lang.Thread.run(Thread.java:662)
  160. Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values ('Amsterdam', 'NL', 'foo@bar.com', 'Guy with cashout', '1', '1') was aborted.  Call getNextException to see the cause.
  161.     at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2598)
  162.     at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
  163.     at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
  164.     at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2737)
  165.     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
  166.     at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
  167.     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
  168.     ... 27 more
  169. 2011-09-08 14:10:45,428 ERROR Batch entry 0 insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values ('Amsterdam', 'NL', 'foo@bar.com', 'Guy with cashout', '1', '1') was aborted.  Call getNextException to see the cause.
  170. 2011-09-08 14:10:45,428 ERROR ERROR: current transaction is aborted, commands ignored until end of transaction block
  171. 2011-09-08 14:10:45,428 ERROR Could not synchronize database state with session
  172. org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
  173.     at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
  174.     at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
  175.     at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
  176.     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
  177.     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
  178.     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
  179.     at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
  180.     at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2411)
  181.     at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2874)
  182.     at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
  183.     at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
  184.     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
  185.     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
  186.     at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:345)
  187.     at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
  188.     at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
  189.     at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795)
  190.     at play.db.jpa.JPABase._save(JPABase.java:47)
  191.     at play.db.jpa.GenericModel.save(GenericModel.java:184)
  192.     at jobs.SubscribersImportJob.importEmails(SubscribersImportJob.java:92)
  193.     at jobs.SubscribersImportJob.doJob(SubscribersImportJob.java:44)
  194.     at play.jobs.Job.doJobWithResult(Job.java:50)
  195.     at play.jobs.Job.call(Job.java:146)
  196.     at play.jobs.Job$2.call(Job.java:94)
  197.     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
  198.     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  199.     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
  200.     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
  201.     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  202.     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  203.     at java.lang.Thread.run(Thread.java:662)
  204. Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values ('Amsterdam', 'NL', 'foo@bar.com', 'Guy with cashout', '1', '1') was aborted.  Call getNextException to see the cause.
  205.     at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2598)
  206.     at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
  207.     at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
  208.     at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2737)
  209.     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
  210.     at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
  211.     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
  212.     ... 27 more
  213. 2011-09-08 14:10:45,428 ERROR
  214.  
  215. @67k9cdeml
  216. Error during job execution (jobs.SubscribersImportJob)
  217.  
  218. Execution exception (In /app/jobs/SubscribersImportJob.java around line 92)
  219. PersistenceException occured : insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values (?, ?, ?, ?, ?, ?)
  220.  
  221. play.exceptions.JavaExecutionException: insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values (?, ?, ?, ?, ?, ?)
  222.     at play.jobs.Job.call(Job.java:155)
  223.     at play.jobs.Job$2.call(Job.java:94)
  224.     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
  225.     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  226.     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
  227.     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
  228.     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  229.     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  230.     at java.lang.Thread.run(Thread.java:662)
  231. Caused by: javax.persistence.PersistenceException: insert into "Subscriber" ("city", "country", "email", "name", "needSync", "id") values (?, ?, ?, ?, ?, ?)
  232.     at play.db.jpa.JPABase._save(JPABase.java:50)
  233.     at play.db.jpa.GenericModel.save(GenericModel.java:184)
  234.     at jobs.SubscribersImportJob.importEmails(SubscribersImportJob.java:92)
  235.     at jobs.SubscribersImportJob.doJob(SubscribersImportJob.java:44)
  236.     at play.jobs.Job.doJobWithResult(Job.java:50)
  237.     at play.jobs.Job.call(Job.java:146)
  238.     ... 8 more
  239.  
  240.  
  241.  
  242. */
Add Comment
Please, Sign In to add comment