Example usage for org.apache.commons.lang.time DateUtils addSeconds

List of usage examples for org.apache.commons.lang.time DateUtils addSeconds

Introduction

In this page you can find the example usage for org.apache.commons.lang.time DateUtils addSeconds.

Prototype

public static Date addSeconds(Date date, int amount) 

Source Link

Document

Adds a number of seconds to a date returning a new object.

Usage

From source file:org.cleverbus.core.common.asynch.queue.MessagePollExecutorTest.java

@Test
@Transactional//from   w w w .  ja  v  a 2s  .  c o  m
public void testGuaranteedOrder_multiFunnelMessage() throws InterruptedException {
    // prepare message that should be postponed
    insertNewMessage("id1", MsgStateEnum.PROCESSING, true, FUNNEL_VALUE, FUNNEL_VALUE_TWO);

    //first message
    Message msg = insertNewMessage("id2", MsgStateEnum.PROCESSING, true, FUNNEL_VALUE);
    msg.setReceiveTimestamp(DateUtils.addSeconds(new Date(), 10)); //postponedIntervalWhenFailed=0

    // action
    messagePollExecutor.startMessageProcessing(msg);

    Assert.assertThat(em.find(Message.class, msg.getMsgId()).getState(),
            CoreMatchers.is(MsgStateEnum.POSTPONED));

    //second message
    msg = insertNewMessage("id3", MsgStateEnum.PROCESSING, true, FUNNEL_VALUE, FUNNEL_VALUE_TWO);
    msg.setReceiveTimestamp(DateUtils.addSeconds(new Date(), 100));

    // action
    messagePollExecutor.startMessageProcessing(msg);

    Assert.assertThat(em.find(Message.class, msg.getMsgId()).getState(),
            CoreMatchers.is(MsgStateEnum.POSTPONED));

    //third message
    msg = insertNewMessage("id4", MsgStateEnum.PROCESSING, true, FUNNEL_VALUE_TWO);
    msg.setReceiveTimestamp(DateUtils.addSeconds(new Date(), 200));

    // action
    messagePollExecutor.startMessageProcessing(msg);

    Assert.assertThat(em.find(Message.class, msg.getMsgId()).getState(),
            CoreMatchers.is(MsgStateEnum.POSTPONED));

    //fifth message
    msg = insertNewMessage("id5", MsgStateEnum.PROCESSING, true, "someOtherValue", "someOtherValueTwo",
            "someValue");
    msg.setReceiveTimestamp(DateUtils.addSeconds(new Date(), 300));

    // action
    messagePollExecutor.startMessageProcessing(msg);

    Assert.assertThat(em.find(Message.class, msg.getMsgId()).getState(),
            CoreMatchers.is(MsgStateEnum.PROCESSING));
}

From source file:org.cleverbus.core.common.dao.ExternalCallDaoJpaImpl.java

@Override
@Nullable/*from w  ww . j  a v a 2 s  .co  m*/
@SuppressWarnings("unchecked")
public ExternalCall findConfirmation(int interval) {
    // find confirmation that was lastly processed before specified interval
    Date lastUpdateLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT c " + "FROM " + ExternalCall.class.getName() + " c "
            + "WHERE c.operationName = :operationName" + "     AND c.state = :state"
            + "     AND c.lastUpdateTimestamp < :lastUpdateTimestamp" + " ORDER BY c.creationTimestamp";

    TypedQuery<ExternalCall> q = em.createQuery(jSql, ExternalCall.class);
    q.setParameter("operationName", ExternalCall.CONFIRM_OPERATION);
    q.setParameter("state", ExternalCallStateEnum.FAILED);
    q.setParameter("lastUpdateTimestamp", new Timestamp(lastUpdateLimit.getTime()));
    q.setMaxResults(1);
    List<ExternalCall> extCalls = q.getResultList();

    if (extCalls.isEmpty()) {
        return null;
    } else {
        return extCalls.get(0);
    }
}

From source file:org.cleverbus.core.common.dao.ExternalCallDaoJpaImpl.java

@Override
public List<ExternalCall> findProcessingExternalCalls(int interval) {
    final Date startProcessLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT c " + "FROM " + ExternalCall.class.getName() + " c " + "WHERE c.state = '"
            + ExternalCallStateEnum.PROCESSING + "'" + "     AND c.lastUpdateTimestamp < :time";

    TypedQuery<ExternalCall> q = em.createQuery(jSql, ExternalCall.class);
    q.setParameter("time", new Timestamp(startProcessLimit.getTime()));
    q.setMaxResults(MAX_MESSAGES_IN_ONE_QUERY);
    return q.getResultList();
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
@Nullable/*from  ww w  .  j  a  v  a  2s . co m*/
public Message findPartlyFailedMessage(int interval) {
    // find message that was lastly processed before specified interval
    Date lastUpdateLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m " + "WHERE m.state = '"
            + MsgStateEnum.PARTLY_FAILED + "'" + "     AND m.lastUpdateTimestamp < :lastTime"
            + " ORDER BY m.msgTimestamp";

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("lastTime", new Timestamp(lastUpdateLimit.getTime()));
    q.setMaxResults(1);
    List<Message> messages = q.getResultList();

    if (messages.isEmpty()) {
        return null;
    } else {
        return messages.get(0);
    }
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
@Nullable/*  w ww . j  a  v  a 2s. c o  m*/
public Message findPostponedMessage(int interval) {
    // find message that was lastly processed before specified interval
    Date lastUpdateLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m " + "WHERE m.state = '"
            + MsgStateEnum.POSTPONED + "'" + "     AND m.lastUpdateTimestamp < :lastTime"
            + " ORDER BY m.msgTimestamp";

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("lastTime", new Timestamp(lastUpdateLimit.getTime()));
    q.setMaxResults(1);
    List<Message> messages = q.getResultList();

    if (messages.isEmpty()) {
        return null;
    } else {
        return messages.get(0);
    }
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
public List<Message> findProcessingMessages(int interval) {
    final Date startProcessLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m " + "WHERE m.state = '"
            + MsgStateEnum.PROCESSING + "'" + "     AND m.startProcessTimestamp < :startTime";

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("startTime", new Timestamp(startProcessLimit.getTime()));
    q.setMaxResults(MAX_MESSAGES_IN_ONE_QUERY);
    return q.getResultList();
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
public int getCountMessages(MsgStateEnum state, Integer interval) {
    Date lastUpdateTime = null;/*from ww  w. j av  a2  s.co  m*/

    String jSql = "SELECT COUNT(m) " + "FROM " + Message.class.getName() + " m " + "WHERE m.state = '"
            + state.name() + "'";

    if (interval != null) {
        lastUpdateTime = DateUtils.addSeconds(new Date(), -interval);
        jSql += " AND m.lastUpdateTimestamp >= :lastUpdateTime";
    }

    TypedQuery<Number> q = em.createQuery(jSql, Number.class);
    if (lastUpdateTime != null) {
        q.setParameter("lastUpdateTime", new Timestamp(lastUpdateTime.getTime()));
    }

    return q.getSingleResult().intValue();
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
public int getCountProcessingMessagesForFunnel(Collection<String> funnelValues, int idleInterval,
        String funnelCompId) {/*from w  ww .  j a v  a 2s  .com*/
    Assert.notEmpty(funnelValues, "funnelValues must not be empty");
    Assert.hasText(funnelCompId, "funnelCompId must not be empty");

    String jSql = "SELECT COUNT(m) " + "FROM " + Message.class.getName() + " m " + "INNER JOIN m.funnels f "
            + "WHERE (m.state = '" + MsgStateEnum.PROCESSING + "' " + "         OR m.state = '"
            + MsgStateEnum.WAITING + "'" + "         OR m.state = '" + MsgStateEnum.WAITING_FOR_RES + "')"
            + "      AND m.startProcessTimestamp >= :startTime" + "      AND m.funnelComponentId = '"
            + funnelCompId + "'" + "      AND (";

    for (String funnelValue : funnelValues) {
        jSql += "f.funnelValue = '" + funnelValue + "' OR ";
    }
    //remove last or
    jSql = StringUtils.substringBeforeLast(jSql, " OR ");

    jSql += ")";

    TypedQuery<Number> q = em.createQuery(jSql, Number.class);
    q.setParameter("startTime", new Timestamp(DateUtils.addSeconds(new Date(), -idleInterval).getTime()));

    return q.getSingleResult().intValue();
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
public List<Message> getMessagesForGuaranteedOrderForFunnel(Collection<String> funnelValues, int idleInterval,
        boolean excludeFailedState, String funnelCompId) {
    Assert.notNull(funnelValues, "funnelValues must not be null");
    Assert.hasText(funnelCompId, "funnelCompId must not be empty");

    if (funnelValues.isEmpty()) {
        return Collections.emptyList();
    } else {//from   w  ww .j  a  va  2s  . c o  m
        String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m " + "INNER JOIN m.funnels f "
                + "WHERE (m.state = '" + MsgStateEnum.PROCESSING + "' " + "         OR m.state = '"
                + MsgStateEnum.WAITING + "'" + "         OR m.state = '" + MsgStateEnum.PARTLY_FAILED + "'"
                + "         OR m.state = '" + MsgStateEnum.POSTPONED + "'";

        if (!excludeFailedState) {
            jSql += "         OR m.state = '" + MsgStateEnum.FAILED + "'";
        }

        jSql += "         OR m.state = '" + MsgStateEnum.WAITING_FOR_RES + "')"
                + "      AND m.funnelComponentId = '" + funnelCompId + "'"
                + "      AND m.startProcessTimestamp >= :startTime" + "      AND (";

        for (String funnelValue : funnelValues) {
            jSql += "f.funnelValue = '" + funnelValue + "' OR ";
        }
        //remove last or
        jSql = StringUtils.substringBeforeLast(jSql, " OR ");

        jSql += ") ORDER BY m.msgTimestamp";

        //TODO (juza) limit select to specific number of items + add msgId DESC to sorting (parent vs. child)

        TypedQuery<Message> q = em.createQuery(jSql, Message.class);
        q.setParameter("startTime", new Timestamp(DateUtils.addSeconds(new Date(), -idleInterval).getTime()));

        return q.getResultList();
    }
}

From source file:org.cleverbus.core.throttling.ThrottleCounterMemoryImpl.java

@Override
public int count(ThrottleScope throttleScope, int interval) {
    Assert.notNull(throttleScope, "the throttleScope must not be null");
    Assert.isTrue(interval > 0, "the interval must be positive value");

    int counter = 0;
    boolean toLock = false;

    // is it necessary to lock thread? Only if two same throttle scopes are processed at the same time
    synchronized (OBJ_LOCK) {
        if (scopesInProgress.contains(throttleScope)) {
            toLock = true;//from   w ww  . j  a  va  2  s .  c om
        } else {
            scopesInProgress.add(throttleScope);
        }
    }

    if (toLock) {
        lock.lock();
    }

    try {
        if (requests.get(throttleScope) == null) {
            requests.put(throttleScope, new Stack<Long>());
        }

        long now = DateTime.now().getMillis();
        long from = now - (interval * 1000);

        // get timestamps for specified throttling scope
        List<Long> timestamps = requests.get(throttleScope);
        timestamps.add(now);

        // count requests for specified interval
        int lastIndex = -1;
        for (int i = timestamps.size() - 1; i >= 0; i--) {
            long timestamp = timestamps.get(i);

            if (timestamp >= from) {
                counter++;
            } else {
                lastIndex = i;
                break;
            }
        }

        // remove old timestamps
        if (lastIndex > 0) {
            for (int i = 0; i <= lastIndex; i++) {
                timestamps.remove(0);
            }
        }
    } finally {
        synchronized (OBJ_LOCK) {
            scopesInProgress.remove(throttleScope);
        }

        if (toLock) {
            lock.unlock();
        }
    }

    // make dump only once in the specified interval
    if (Log.isDebugEnabled() && (DateUtils.addSeconds(new Date(), -DUMP_PERIOD).after(lastDumpTimestamp))) {
        dumpMemory();

        lastDumpTimestamp = new Date();
    }

    return counter;
}