Search in sources :

Example 1 with AQDequeueOption

use of oracle.AQ.AQDequeueOption in project autonomous-database-for-developers by loiclefevre.

the class AQDequeueService method run.

@Override
public void run() {
    try {
        final AQQueue queue = aqSessionForDequeue.getQueue(ociConfiguration.getDatabaseUsername(), "AQ_NOTIFICATIONS_QUEUE");
        final AQDequeueOption dequeueOption = new AQDequeueOption();
        // timeout after 2 seconds
        dequeueOption.setWaitTime(2);
        try {
            while (isRunning()) {
                try {
                    final Event event = getMessage(queue, dequeueOption);
                    if (event.priority == AQEnqueueService.HIGH_PRIORITY) {
                        LOG.warn("Thread {} received HIGH priority message: {}", Thread.currentThread().getName(), event.message());
                    } else {
                        LOG.warn("Thread {} received message: {}", Thread.currentThread().getName(), event.message());
                    }
                } catch (AQException e) {
                    if (e.getErrorCode() != AQ_TIMEOUT_ERROR_CODE && e.getErrorCode() != AQ_DEQUEUE_DISABLED) {
                        throw e;
                    }
                }
            }
        } finally {
            // and rollback before!
            try {
                aqSessionForDequeue.getDBConnection().rollback();
            } catch (SQLException ignored) {
            }
            try {
                queue.stopEnqueue(false);
                queue.stopDequeue(false);
            } catch (AQException ignored) {
            }
        }
    } catch (AQException | SQLException e) {
        LOG.error("While dequeuing!", e);
    } finally {
        if (aqSessionForDequeue != null) {
            LOG.info("Closing AQ session...");
            aqSessionForDequeue.close();
        }
    }
}
Also used : SQLException(java.sql.SQLException) AQQueue(oracle.AQ.AQQueue) AQDequeueOption(oracle.AQ.AQDequeueOption) AQException(oracle.AQ.AQException)

Example 2 with AQDequeueOption

use of oracle.AQ.AQDequeueOption in project eclipselink by eclipse-ee4j.

the class OrderQueueTest method testReadTimeout.

/**
 * Reading with a dequeue timeout test.
 */
@Test
public void testReadTimeout() throws Exception {
    final DatabaseSession session = SessionHelper.createDatabaseSession(AQTestSuite.project);
    XMLInteraction interaction = new XMLInteraction();
    AQDequeueOption options = new AQDequeueOption();
    options.setWaitTime(1);
    interaction.setProperty(AQPlatform.QUEUE_OPERATION, AQPlatform.DEQUEUE);
    interaction.setProperty(AQPlatform.DEQUEUE_OPTIONS, options);
    boolean timeout = false;
    try {
        session.readObject(org.eclipse.persistence.testing.models.order.Order.class, interaction);
    } catch (EISException exception) {
        timeout = true;
        if (exception.getMessage().indexOf("timeout") == -1) {
            throw exception;
        }
    } finally {
        session.logout();
    }
    if (!timeout) {
        throw new TestErrorException("Timeout exception did not occur, was a message in the queue.");
    }
}
Also used : DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) EISException(org.eclipse.persistence.eis.EISException) XMLInteraction(org.eclipse.persistence.eis.interactions.XMLInteraction) AQDequeueOption(oracle.AQ.AQDequeueOption) Test(org.junit.Test)

Example 3 with AQDequeueOption

use of oracle.AQ.AQDequeueOption in project eclipselink by eclipse-ee4j.

the class JavaDirectInteractionTest method testDirectInteraction.

/**
 * Test AQ Java driver direct interactions.
 */
@Test
public void testDirectInteraction() throws Exception {
    // Only this test uses "raw_order_queue" in this class so relational database session is local.
    final DatabaseSession rDBSession = SessionHelper.createDatabaseSession(SessionHelper.createModelProject(SessionHelper.createDatabaseLogin(), JavaDirectInteractionTest.class));
    ModelHelper.setupRawOrderQueue(rDBSession);
    Connection connection = null;
    AQSession session = null;
    try {
        connection = getConnection();
        connection.setAutoCommit(false);
        Class.forName("oracle.AQ.AQOracleDriver");
        session = AQDriverManager.createAQSession(connection);
        AQQueue queue = session.getQueue(NoSQLProperties.getDBUserName(), "raw_order_queue");
        LOG.log(SessionLog.FINEST, queue.toString());
        AQMessage message = queue.createMessage();
        LOG.log(SessionLog.FINEST, message.toString());
        AQRawPayload payload = message.getRawPayload();
        byte[] bytes = "hello".getBytes();
        payload.setStream(bytes, bytes.length);
        AQEnqueueOption enqueueOption = new AQEnqueueOption();
        queue.enqueue(enqueueOption, message);
        connection.commit();
        AQDequeueOption dequeueOption = new AQDequeueOption();
        message = queue.dequeue(dequeueOption);
        LOG.log(SessionLog.FINEST, message != null ? message.toString() : null);
        LOG.log(SessionLog.FINEST, message != null ? new String(message.getRawPayload().getBytes()) : null);
        connection.commit();
    } finally {
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
        ModelHelper.resetRawOrderQueue(rDBSession);
        rDBSession.logout();
    }
}
Also used : DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) AQMessage(oracle.AQ.AQMessage) AQEnqueueOption(oracle.AQ.AQEnqueueOption) Connection(java.sql.Connection) AQQueue(oracle.AQ.AQQueue) AQRawPayload(oracle.AQ.AQRawPayload) AQDequeueOption(oracle.AQ.AQDequeueOption) AQSession(oracle.AQ.AQSession) Test(org.junit.Test)

Aggregations

AQDequeueOption (oracle.AQ.AQDequeueOption)3 AQQueue (oracle.AQ.AQQueue)2 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)2 Test (org.junit.Test)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 AQEnqueueOption (oracle.AQ.AQEnqueueOption)1 AQException (oracle.AQ.AQException)1 AQMessage (oracle.AQ.AQMessage)1 AQRawPayload (oracle.AQ.AQRawPayload)1 AQSession (oracle.AQ.AQSession)1 EISException (org.eclipse.persistence.eis.EISException)1 XMLInteraction (org.eclipse.persistence.eis.interactions.XMLInteraction)1 TestErrorException (org.eclipse.persistence.testing.framework.TestErrorException)1