Example usage for org.springframework.context ApplicationEventPublisher publishEvent

List of usage examples for org.springframework.context ApplicationEventPublisher publishEvent

Introduction

In this page you can find the example usage for org.springframework.context ApplicationEventPublisher publishEvent.

Prototype

void publishEvent(Object event);

Source Link

Document

Notify all matching listeners registered with this application of an event.

Usage

From source file:org.grails.orm.hibernate.HibernateSession.java

/**
 * Deletes all objects matching the given criteria.
 *
 * @param criteria The criteria/*from w  w w  .  j ava2  s.c o  m*/
 * @return The total number of records deleted
 */
public long deleteAll(final QueryableCriteria criteria) {
    return getHibernateTemplate().execute(new GrailsHibernateTemplate.HibernateCallback<Integer>() {
        public Integer doInHibernate(Session session) throws HibernateException, SQLException {
            JpaQueryBuilder builder = new JpaQueryBuilder(criteria);
            builder.setHibernateCompatible(true);
            JpaQueryInfo jpaQueryInfo = builder.buildDelete();

            org.hibernate.Query query = session.createQuery(jpaQueryInfo.getQuery());
            getHibernateTemplate().applySettings(query);

            List parameters = jpaQueryInfo.getParameters();
            if (parameters != null) {
                for (int i = 0, count = parameters.size(); i < count; i++) {
                    query.setParameter(JpaQueryBuilder.PARAMETER_NAME_PREFIX + (i + 1), parameters.get(i));
                }
            }

            HibernateHqlQuery hqlQuery = new HibernateHqlQuery(HibernateSession.this,
                    criteria.getPersistentEntity(), query);
            ApplicationEventPublisher applicationEventPublisher = datastore.getApplicationEventPublisher();
            applicationEventPublisher.publishEvent(new PreQueryEvent(datastore, hqlQuery));
            int result = query.executeUpdate();
            applicationEventPublisher
                    .publishEvent(new PostQueryEvent(datastore, hqlQuery, Collections.singletonList(result)));
            return result;
        }
    });
}

From source file:org.grails.orm.hibernate.HibernateSession.java

/**
 * Updates all objects matching the given criteria and property values.
 *
 * @param criteria The criteria//from w  ww .j  a v a  2  s. c o  m
 * @param properties The properties
 * @return The total number of records updated
 */
public long updateAll(final QueryableCriteria criteria, final Map<String, Object> properties) {
    return getHibernateTemplate().execute(new GrailsHibernateTemplate.HibernateCallback<Integer>() {
        public Integer doInHibernate(Session session) throws HibernateException, SQLException {
            JpaQueryBuilder builder = new JpaQueryBuilder(criteria);
            builder.setHibernateCompatible(true);
            PersistentEntity targetEntity = criteria.getPersistentEntity();
            PersistentProperty lastUpdated = targetEntity.getPropertyByName(GormProperties.LAST_UPDATED);
            if (lastUpdated != null && targetEntity.getMapping().getMappedForm().isAutoTimestamp()) {
                properties.put(GormProperties.LAST_UPDATED, new Date());
            }

            JpaQueryInfo jpaQueryInfo = builder.buildUpdate(properties);

            org.hibernate.Query query = session.createQuery(jpaQueryInfo.getQuery());
            getHibernateTemplate().applySettings(query);
            List parameters = jpaQueryInfo.getParameters();
            if (parameters != null) {
                for (int i = 0, count = parameters.size(); i < count; i++) {
                    query.setParameter(JpaQueryBuilder.PARAMETER_NAME_PREFIX + (i + 1), parameters.get(i));
                }
            }

            HibernateHqlQuery hqlQuery = new HibernateHqlQuery(HibernateSession.this, targetEntity, query);
            ApplicationEventPublisher applicationEventPublisher = datastore.getApplicationEventPublisher();
            applicationEventPublisher.publishEvent(new PreQueryEvent(datastore, hqlQuery));
            int result = query.executeUpdate();
            applicationEventPublisher
                    .publishEvent(new PostQueryEvent(datastore, hqlQuery, Collections.singletonList(result)));
            return result;
        }
    });
}

From source file:io.lavagna.config.DataSourceConfig.java

@Bean
public DatabaseMigrator migrator(Environment env, DataSource dataSource, ApplicationEventPublisher publisher) {

    boolean isDev = ArrayUtils.contains(env.getActiveProfiles(), "dev");

    DatabaseMigrator migrator = new DatabaseMigrator(env, dataSource,
            isDev ? MigrationVersion.LATEST : LATEST_STABLE_VERSION);
    publisher.publishEvent(new DatabaseMigrationDoneEvent(this));
    return migrator;
}

From source file:devbury.dewey.hipchat.MessagePacketListenerTest.java

@Test
public void handlePacketChat(@Mocked UserManager userManager,
        @Mocked ApplicationEventPublisher eventPublisher) {
    UserEntry userEntry = new UserEntry();
    userEntry.setName("name");
    userEntry.setMentionName("mention_name");

    new NonStrictExpectations() {
        {/*w  ww  . j  a  v a 2s . c  om*/
            userManager.findUserEntryById("900");
            result = userEntry;
        }
    };

    victim.setUserManager(userManager);
    victim.setEventPublisher(eventPublisher);

    HipChatSettings hipChatSettings = new HipChatSettings();
    hipChatSettings.setMentionName("my_mention_name");

    victim.setHipChatSettings(hipChatSettings);

    Message message = new Message();
    message.setFrom("group_900@chat.hipchat.com/morestuff");
    message.setBody("body");
    message.setType(Message.Type.chat);

    victim.handlePacket(message);

    new Verifications() {
        {
            MessageEvent e;
            eventPublisher.publishEvent(e = withCapture());
            assertTrue(e.getMessage().isToMe());
            assertEquals("name", e.getMessage().getFrom().getName());
            assertEquals("body", e.getMessage().getBody());
        }
    };
}

From source file:devbury.dewey.hipchat.MessagePacketListenerTest.java

@Test
public void handlePacketGroupChat(@Mocked UserManager userManager,
        @Mocked ApplicationEventPublisher eventPublisher) {
    UserEntry userEntry = new UserEntry();
    userEntry.setName("Some User");
    userEntry.setMentionName("mention_name");

    new NonStrictExpectations() {
        {//  w  w  w.  j  a v  a  2  s.co  m
            userManager.findUserEntryByName("Some User");
            result = userEntry;
        }
    };

    victim.setUserManager(userManager);
    victim.setEventPublisher(eventPublisher);

    HipChatSettings hipChatSettings = new HipChatSettings();
    hipChatSettings.setMentionName("my_mention_name");

    victim.setHipChatSettings(hipChatSettings);

    Message message = new Message();
    message.setFrom("somestring_group_name@hipchat.com/Some User");
    message.setBody("@my_mention_name body");
    message.setType(Message.Type.groupchat);

    victim.handlePacket(message);

    new Verifications() {
        {
            MessageEvent e;
            eventPublisher.publishEvent(e = withCapture());
            assertTrue(e.getMessage().isToGroup());
            assertEquals("group_name", e.getMessage().getGroup().getName());
            assertEquals("Some User", e.getMessage().getFrom().getName());
            assertEquals("@my_mention_name body", e.getMessage().getBody());
            assertTrue(e.getMessage().isDirectedToMe());
        }
    };
}

From source file:org.alfresco.repo.batch.BatchProcessor.java

/**
 * Instantiates a new batch processor./*from  w w  w.  j  av a  2  s  .c  o  m*/
 * 
 * @param processName
 *            the process name
 * @param retryingTransactionHelper
 *            the retrying transaction helper
 * @param workProvider
 *            the object providing the work packets
 * @param workerThreads
 *            the number of worker threads
 * @param batchSize
 *            the number of entries we process at a time in a transaction
 * @param applicationEventPublisher
 *            the application event publisher (may be <tt>null</tt>)
 * @param logger
 *            the logger to use (may be <tt>null</tt>)
 * @param loggingInterval
 *            the number of entries to process before reporting progress
 *            
 * @since 3.4 
 */
public BatchProcessor(String processName, RetryingTransactionHelper retryingTransactionHelper,
        BatchProcessWorkProvider<T> workProvider, int workerThreads, int batchSize,
        ApplicationEventPublisher applicationEventPublisher, Log logger, int loggingInterval) {
    this.threadFactory = new TraceableThreadFactory();
    this.threadFactory.setNamePrefix(processName);
    this.threadFactory.setThreadDaemon(true);

    this.processName = processName;
    this.retryingTransactionHelper = retryingTransactionHelper;
    this.workProvider = workProvider;
    this.workerThreads = workerThreads;
    this.batchSize = batchSize;
    if (logger == null) {
        this.logger = LogFactory.getLog(this.getClass());
    } else {
        this.logger = logger;
    }
    this.loggingInterval = loggingInterval;

    // Let the (enterprise) monitoring side know of our presence
    if (applicationEventPublisher != null) {
        applicationEventPublisher.publishEvent(new BatchMonitorEvent(this));
    }
}

From source file:org.grails.datastore.mapping.core.AbstractDatastore.java

private void publishSessionCreationEvent(Session session) {
    ApplicationEventPublisher applicationEventPublisher = getApplicationEventPublisher();
    if (applicationEventPublisher != null) {
        applicationEventPublisher.publishEvent(new SessionCreationEvent(session));
    }/*from   ww w . jav  a2 s . c om*/
}

From source file:org.springframework.web.socket.messaging.StompSubProtocolHandler.java

private void publishEvent(ApplicationEventPublisher publisher, ApplicationEvent event) {
    try {//  ww  w  .ja va2  s .co m
        publisher.publishEvent(event);
    } catch (Throwable ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Error publishing " + event, ex);
        }
    }
}