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();
}
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());
}
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;
}
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);
}
}
}
}
}
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();
}
Aggregations