我想实现一个轮询服务,该服务应该命中特定的表以获取从任何其他应用程序插入的最新记录,或者可以通过在SQL提示符下直接插入查询来实现。它应该在监听所有插入的表,或者我应该每隔几秒钟点击一次表以获取新记录。请通过轮询服务或侦听器或多线程来帮助我实现该目标。
我创建了一个侦听器,如下所示:-
@Component
public class SMSListener implements ApplicationListener<ApplicationEvent>, PostInsertEventListener{
@Autowired
SMSServiceImpl serviceImpl;
@Autowired
SendSMS sendSMS;
public SMSListener() {
}
@Override
public boolean requiresPostCommitHanding(EntityPersister persister) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onPostInsert(PostInsertEvent event) {
log.debug("Entering SMSListener:onPostInsert:::::::::::::::");
try {
sendSMS.sendSMSes();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("event: " + event);
}}
和一个EntityEventListenerRegistry类:-
@Component
public class EntityEventListenerRegistry {
private static final Logger logger = LoggerFactory.getLogger(
EntityEventListenerRegistry.class);
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
return buildSessionFactory();
}
private static SessionFactory buildSessionFactory() {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
return sessionFactory;
}
/**
* EventListenerRegistry:
* http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/
* Hibernate_User_Guide.html#annotations-jpa-entitylisteners
*/
@SuppressWarnings("unchecked")
@PostConstruct
public void registerListeners(){
logger.info("Registering event listeners::::::::::::::::::");
EventListenerRegistry eventListenerRegistry = ((SessionFactoryImplementor)sessionFactory.getSessionFactory()).
getServiceRegistry().getService(EventListenerRegistry.class);
eventListenerRegistry.appendListeners(EventType.POST_INSERT, SMSListener.class);
}}
并在模型类的侦听器条目下方:-
@Entity
@Table(name = "ozekimessageout", schema = "dbo")
@EntityListeners(SMSListener.class)
public class SMSOutgoing implements Serializable
和应用程序一样,主要方法是:-
@SpringBootApplication(scanBasePackages = "com.avaal.sms.*", exclude=
{HibernateJpaAutoConfiguration.class})
@Configuration
@EnableJpaRepositories(basePackages = "com.avaal.sms.*")
@ComponentScan
@EnableAutoConfiguration
public static void main(String[] args) throws PlivoXmlException {
SpringApplication application = new SpringApplication();
application.addListeners(new SMSListener());
SpringApplication.run(Application.class, args);
但是在运行此代码时出现错误:-
05:38:05.787 [main] INFO o.h.jpa.internal.util.LogHelper - HHH000204:
Processing PersistenceUnitInfo [
name: default
...]
05:38:05.829 [main] WARN o.s.w.c.s.GenericWebApplicationContext -
Exception encountered during context initialization - cancelling refresh
attempt: org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'entityManagerFactory' defined in class path
resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]
: Invocation of init method failed; nested exception is
java.util.ServiceConfigurationError:
org.hibernate.integrator.spi.Integrator: Provider
org.hibernate.ejb.event.JpaIntegrator not found
05:38:05.845 [main] INFO
o.s.b.a.l.ConditionEvaluationReportLoggingListener -
Error starting ApplicationContext. To display the conditions report re-run
your application with 'debug' enabled.
05:38:05.845 [main] ERROR o.s.boot.SpringApplication - Application run
failed
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.util.ServiceConfigurationError: org.hibernate.integrator.spi.Integrator: Provider org.hibernate.ejb.event.JpaIntegrator not found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1762)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean