Search in sources :

Example 6 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class TriggerTestCase method testQuery4.

@Test
public void testQuery4() throws InterruptedException {
    log.info("testTrigger4 - OUT 0");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (triggered_time long); " + "define trigger StockStream at 'start' ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams);
    siddhiAppRuntime.start();
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 7 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method test05EdgeCase.

@Test
public void test05EdgeCase() throws Exception {
    siddhiManager = new SiddhiManager();
    // every 10 sec
    SiddhiAppRuntime runtime = simpleQueryRuntime();
    final AtomicInteger recCount = new AtomicInteger(0);
    runtime.addCallback("query", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            AssertJUnit.assertEquals(1, inEvents.length);
            recCount.incrementAndGet();
            double avgCpu = (Double) inEvents[0].getData()[0];
            if (recCount.get() == 1) {
                AssertJUnit.assertEquals(15, avgCpu, 0);
            } else if (recCount.get() == 2) {
                AssertJUnit.assertEquals(85, avgCpu, 0);
            }
            long count = (Long) inEvents[0].getData()[1];
            AssertJUnit.assertEquals(3, count);
        }
    });
    InputHandler input = runtime.getInputHandler("jmxMetric");
    runtime.start();
    // external events' time stamp less than the window, should not have event recieved in call back.
    long now = 0;
    int length = 3;
    for (int i = 0; i < length; i++) {
        input.send(new Object[] { 15, now + i * 10 });
    }
    // if the trigger event mix with the last window, we should see the avgValue is not expected
    for (int i = 0; i < length; i++) {
        // the first entity of the second round
        input.send(new Object[] { 85, now + 10000 + i * 10 });
    }
    // to trigger second round
    input.send(new Object[] { 10000, now + 10 * 10000 });
    // latch.await();// for debug
    Thread.sleep(1000);
    AssertJUnit.assertEquals(2, recCount.get());
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 8 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class SiddhiApp method defineTrigger.

public SiddhiApp defineTrigger(TriggerDefinition triggerDefinition) {
    if (triggerDefinition == null) {
        throw new SiddhiAppValidationException("Trigger Definition should not be null");
    } else if (triggerDefinition.getId() == null) {
        throw new SiddhiAppValidationException("Trigger Id should not be null for Trigger Definition", triggerDefinition.getQueryContextStartIndex(), triggerDefinition.getQueryContextEndIndex());
    }
    StreamDefinition streamDefinition = StreamDefinition.id(triggerDefinition.getId()).attribute(SiddhiConstants.TRIGGERED_TIME, Attribute.Type.LONG);
    streamDefinition.setQueryContextStartIndex(triggerDefinition.getQueryContextStartIndex());
    streamDefinition.setQueryContextEndIndex(triggerDefinition.getQueryContextEndIndex());
    try {
        checkDuplicateDefinition(streamDefinition);
    } catch (DuplicateDefinitionException e) {
        throw new DuplicateDefinitionException("Trigger '" + triggerDefinition.getId() + "' cannot be defined as," + " " + e.getMessageWithOutContext(), e, triggerDefinition.getQueryContextStartIndex(), triggerDefinition.getQueryContextEndIndex());
    }
    if (triggerDefinitionMap.containsKey(triggerDefinition.getId())) {
        throw new DuplicateDefinitionException("Trigger Definition with same Id '" + triggerDefinition.getId() + "' already exist '" + triggerDefinitionMap.get(triggerDefinition.getId()) + "', hence cannot add '" + triggerDefinition + "'", triggerDefinition.getQueryContextStartIndex(), triggerDefinition.getQueryContextEndIndex());
    }
    this.triggerDefinitionMap.put(triggerDefinition.getId(), triggerDefinition);
    this.streamDefinitionMap.put(streamDefinition.getId(), streamDefinition);
    return this;
}
Also used : StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) DuplicateDefinitionException(org.wso2.siddhi.query.api.exception.DuplicateDefinitionException) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)

Example 9 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project carbon-apimgt by wso2.

the class FunctionTrigger method captureEvent.

/**
 * Used to observe all the {@link org.wso2.carbon.apimgt.core.models.Event} occurrences
 * in an {@link org.wso2.carbon.apimgt.core.api.APIMObservable} object and trigger corresponding function that is
 * mapped to the particular {@link org.wso2.carbon.apimgt.core.models.Event} occurred.
 * <p>
 * This is a specific implementation for
 * {@link org.wso2.carbon.apimgt.core.api.EventObserver#captureEvent(Event, String, ZonedDateTime, Map)} method,
 * provided by {@link org.wso2.carbon.apimgt.core.impl.FunctionTrigger} which implements
 * {@link org.wso2.carbon.apimgt.core.api.EventObserver} interface.
 * <p>
 * {@inheritDoc}
 *
 * @see org.wso2.carbon.apimgt.core.impl.EventLogger#captureEvent(Event, String, ZonedDateTime, Map)
 */
@Override
public void captureEvent(Event event, String username, ZonedDateTime eventTime, Map<String, String> metadata) {
    List<Function> functions = null;
    String jsonPayload = null;
    if (event == null) {
        throw new IllegalArgumentException("Event must not be null");
    }
    if (username == null) {
        throw new IllegalArgumentException("Username must not be null");
    }
    if (eventTime == null) {
        throw new IllegalArgumentException("Event_time must not be null");
    }
    if (metadata == null) {
        throw new IllegalArgumentException("Payload must not be null");
    }
    // Add general attributes to payload
    metadata.put(APIMgtConstants.FunctionsConstants.EVENT, event.getEventAsString());
    metadata.put(APIMgtConstants.FunctionsConstants.COMPONENT, event.getComponent().getComponentAsString());
    metadata.put(APIMgtConstants.FunctionsConstants.USERNAME, username);
    metadata.put(APIMgtConstants.FunctionsConstants.EVENT_TIME, eventTime.toString());
    try {
        functions = functionDAO.getUserFunctionsForEvent(username, event);
        jsonPayload = new Gson().toJson(metadata);
    } catch (APIMgtDAOException e) {
        String message = "Error loading functions for event from DB: -event: " + event + " -Username: " + username;
        log.error(message, new APIManagementException("Problem invoking 'getUserFunctionsForEvent' method in " + "'FunctionDAO' ", e, ExceptionCodes.APIMGT_DAO_EXCEPTION));
    }
    if (functions != null && !functions.isEmpty()) {
        for (Function function : functions) {
            HttpResponse response = null;
            try {
                response = restCallUtil.postRequest(function.getEndpointURI(), null, null, Entity.json(jsonPayload), MediaType.APPLICATION_JSON_TYPE, Collections.EMPTY_MAP);
            } catch (APIManagementException e) {
                log.error("Failed to make http request: -function: " + function.getName() + " -endpoint URI: " + function.getEndpointURI() + " -event: " + event + " -Username: " + username, e);
            }
            if (response != null) {
                int responseStatusCode = response.getResponseCode();
                // Benefit of integer division used to ensure all possible success response codes covered
                if (responseStatusCode / 100 == 2) {
                    log.info("Function successfully invoked: " + function.getName() + " -event: " + event + " -Username: " + username + " -Response code: " + responseStatusCode);
                } else {
                    log.error("Problem invoking function: " + function.getName() + " -event: " + event + " -Username: " + username + " -Response code: " + responseStatusCode);
                }
            }
        }
    }
}
Also used : Function(org.wso2.carbon.apimgt.core.models.Function) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Gson(com.google.gson.Gson) HttpResponse(org.wso2.carbon.apimgt.core.models.HttpResponse)

Example 10 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class TriggerTestCase method testQuery3.

@Test(expectedExceptions = DuplicateDefinitionException.class)
public void testQuery3() throws InterruptedException {
    log.info("testTrigger3 - OUT 0");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define trigger StockStream at 'start' ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams);
    siddhiAppRuntime.start();
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)9 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)9 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)9 Event (org.wso2.siddhi.core.event.Event)5 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)4 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)4 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)2 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)2 Gson (com.google.gson.Gson)1 ArrayList (java.util.ArrayList)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 Function (org.wso2.carbon.apimgt.core.models.Function)1 HttpResponse (org.wso2.carbon.apimgt.core.models.HttpResponse)1 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)1 StateEvent (org.wso2.siddhi.core.event.state.StateEvent)1 StreamJunction (org.wso2.siddhi.core.stream.StreamJunction)1 CronTrigger (org.wso2.siddhi.core.trigger.CronTrigger)1