use of org.wso2.siddhi.annotation.Extension in project siddhi by wso2.
the class DefinitionParserHelper method addFunction.
public static void addFunction(SiddhiAppContext siddhiAppContext, final FunctionDefinition functionDefinition) {
Extension extension = new Extension() {
@Override
public String getNamespace() {
return "script";
}
@Override
public String getName() {
return functionDefinition.getLanguage().toLowerCase();
}
};
try {
Script script = (Script) SiddhiClassLoader.loadExtensionImplementation(extension, ScriptExtensionHolder.getInstance(siddhiAppContext));
ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(extension.getNamespace(), extension.getName());
script.setReturnType(functionDefinition.getReturnType());
script.init(functionDefinition.getId(), functionDefinition.getBody(), configReader);
siddhiAppContext.getScriptFunctionMap().put(functionDefinition.getId(), script);
} catch (Throwable t) {
ExceptionUtil.populateQueryContext(t, functionDefinition, siddhiAppContext);
throw t;
}
}
use of org.wso2.siddhi.annotation.Extension in project siddhi by wso2.
the class MultiClientDistributedSink method initTransport.
@Override
public void initTransport(OptionHolder sinkOptionHolder, List<OptionHolder> destinationOptionHolders, Annotation sinkAnnotation, ConfigReader sinkConfigReader, SiddhiAppContext siddhiAppContext) {
String transportType = sinkOptionHolder.validateAndGetStaticValue(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
Extension sinkExtension = DefinitionParserHelper.constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, transportType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK);
destinationOptionHolders.forEach(destinationOption -> {
Sink sink = (Sink) SiddhiClassLoader.loadExtensionImplementation(sinkExtension, SinkExecutorExtensionHolder.getInstance(siddhiAppContext));
destinationOption.merge(sinkOptionHolder);
sink.initOnlyTransport(streamDefinition, destinationOption, sinkConfigReader, siddhiAppContext);
transports.add(sink);
});
}
use of org.wso2.siddhi.annotation.Extension in project siddhi by wso2.
the class ExtensionTestCase method extensionTest5.
@Test
public void extensionTest5() throws InterruptedException, ClassNotFoundException {
log.info("extension test5");
SiddhiManager siddhiManager = new SiddhiManager();
Map<String, String> configMap = new HashMap<>();
configMap.put("email.getAllNew.append.abc", "true");
siddhiManager.setConfigManager(new InMemoryConfigManager(configMap, null));
siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);
siddhiManager.setExtension("email:getAllNew", StringConcatAggregatorString.class);
String cseEventStream = "" + "" + "define stream cseEventStream (symbol string, price float, volume long);";
String query = ("" + "@info(name = 'query1') " + "from cseEventStream " + "select price , email:getAllNew(symbol,'') as toConcat " + "group by volume " + "insert into mailOutput;");
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
count = count + inEvents.length;
if (count == 3) {
AssertJUnit.assertEquals("WSO2ABC-abc", inEvents[inEvents.length - 1].getData(1));
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 700f, 100L });
Thread.sleep(100);
inputHandler.send(new Object[] { "WSO2", 60.5f, 200L });
Thread.sleep(100);
inputHandler.send(new Object[] { "ABC", 60.5f, 200L });
Thread.sleep(100);
AssertJUnit.assertEquals(3, count);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.annotation.Extension in project siddhi by wso2.
the class ExtensionTestCase method extensionTest2.
@Test
public void extensionTest2() throws InterruptedException, ClassNotFoundException {
log.info("extension test2");
SiddhiManager siddhiManager = new SiddhiManager();
siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);
siddhiManager.setExtension("email:getAll", StringConcatAggregatorString.class);
String cseEventStream = "define stream cseEventStream (symbol string, price long, volume long);";
String query = ("@info(name = 'query1') from cseEventStream select symbol , custom:plus(price,volume) as " + "totalCount " + "insert into mailOutput;");
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
for (Event inEvent : inEvents) {
count++;
if (count == 1) {
AssertJUnit.assertEquals(800L, inEvent.getData(1));
} else if (count == 2) {
AssertJUnit.assertEquals(805L, inEvent.getData(1));
} else if (count == 3) {
AssertJUnit.assertEquals(260L, inEvent.getData(1));
}
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 700L, 100L });
inputHandler.send(new Object[] { "WSO2", 605L, 200L });
inputHandler.send(new Object[] { "ABC", 60L, 200L });
Thread.sleep(100);
AssertJUnit.assertEquals(3, count);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.annotation.Extension in project siddhi by wso2.
the class ExtensionTestCase method extensionTest1.
@Test
public void extensionTest1() throws InterruptedException {
log.info("extension test1");
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "define stream cseEventStream (symbol string, price float, volume long);";
String query = ("@info(name = 'query1') from cseEventStream select price , custom:getAll(symbol) as toConcat " + "group by volume insert into mailOutput;");
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
count = count + inEvents.length;
if (count == 3) {
AssertJUnit.assertEquals("WSO2ABC", inEvents[inEvents.length - 1].getData(1));
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 700f, 100L });
Thread.sleep(100);
inputHandler.send(new Object[] { "WSO2", 60.5f, 200L });
Thread.sleep(100);
inputHandler.send(new Object[] { "ABC", 60.5f, 200L });
Thread.sleep(100);
AssertJUnit.assertEquals(3, count);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
Aggregations