use of org.ballerinalang.model.values.BFunctionPointer in project ballerina by ballerina-lang.
the class StartForever method execute.
@Override
public void execute(Context context) {
context.setReturnValues();
String siddhiApp = context.getStringArgument(0);
SiddhiAppRuntime siddhiAppRuntime = StreamingRuntimeManager.getInstance().createSiddhiAppRuntime(siddhiApp);
Set<String> streamIds = siddhiAppRuntime.getStreamDefinitionMap().keySet();
Map<String, InputHandler> streamSpecificInputHandlerMap = new HashMap<>();
for (String streamId : streamIds) {
streamSpecificInputHandlerMap.put(streamId, siddhiAppRuntime.getInputHandler(streamId));
}
BRefValueArray inputStreamReferenceArray = (BRefValueArray) context.getRefArgument(0);
BRefValueArray functionPointerArray = (BRefValueArray) context.getRefArgument(4);
for (int i = 0; i < inputStreamReferenceArray.size(); i++) {
BStream stream = (BStream) inputStreamReferenceArray.get(i);
InputHandler inputHandler = streamSpecificInputHandlerMap.get(stream.getStreamId());
stream.subscribe(inputHandler);
}
for (int i = 0; i < functionPointerArray.size(); i++) {
BFunctionPointer functionPointer = (BFunctionPointer) functionPointerArray.get(i);
String functionName = functionPointer.value().getFunctionName();
String streamId = "stream" + functionName.replaceAll("\\$", "_");
StreamingRuntimeManager.getInstance().addCallback(streamId, functionPointer, siddhiAppRuntime);
}
}
use of org.ballerinalang.model.values.BFunctionPointer in project ballerina by ballerina-lang.
the class CPU method beginTransaction.
private static void beginTransaction(WorkerExecutionContext ctx, int transactionBlockId, int retryCountRegIndex, int committedFuncIndex, int abortedFuncIndex) {
// If global tx enabled, it is managed via transaction coordinator. Otherwise it is managed locally without
// any interaction with the transaction coordinator.
boolean isGlobalTransactionEnabled = ctx.getGlobalTransactionEnabled();
// Transaction is attempted three times by default to improve resiliency
int retryCount = TransactionConstants.DEFAULT_RETRY_COUNT;
if (retryCountRegIndex != -1) {
retryCount = (int) ctx.workerLocal.longRegs[retryCountRegIndex];
if (retryCount < 0) {
ctx.setError(BLangVMErrors.createError(ctx, BLangExceptionHelper.getErrorMessage(RuntimeErrors.INVALID_RETRY_COUNT)));
handleError(ctx);
return;
}
}
// Register committed function handler if exists.
if (committedFuncIndex != -1) {
FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) ctx.constPool[committedFuncIndex];
BFunctionPointer fpCommitted = new BFunctionPointer(funcRefCPEntry);
TransactionResourceManager.getInstance().registerCommittedFunction(transactionBlockId, fpCommitted);
}
// Register aborted function handler if exists.
if (abortedFuncIndex != -1) {
FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) ctx.constPool[abortedFuncIndex];
BFunctionPointer fpAborted = new BFunctionPointer(funcRefCPEntry);
TransactionResourceManager.getInstance().registerAbortedFunction(transactionBlockId, fpAborted);
}
LocalTransactionInfo localTransactionInfo = ctx.getLocalTransactionInfo();
if (localTransactionInfo == null) {
String globalTransactionId;
String protocol = null;
String url = null;
if (isGlobalTransactionEnabled) {
BValue[] returns = TransactionUtils.notifyTransactionBegin(ctx, null, null, transactionBlockId, TransactionConstants.DEFAULT_COORDINATION_TYPE);
BStruct txDataStruct = (BStruct) returns[0];
globalTransactionId = txDataStruct.getStringField(1);
protocol = txDataStruct.getStringField(2);
url = txDataStruct.getStringField(3);
} else {
globalTransactionId = UUID.randomUUID().toString().replaceAll("-", "");
}
localTransactionInfo = new LocalTransactionInfo(globalTransactionId, url, protocol);
ctx.setLocalTransactionInfo(localTransactionInfo);
} else {
if (isGlobalTransactionEnabled) {
TransactionUtils.notifyTransactionBegin(ctx, localTransactionInfo.getGlobalTransactionId(), localTransactionInfo.getURL(), transactionBlockId, localTransactionInfo.getProtocol());
}
}
localTransactionInfo.beginTransactionBlock(transactionBlockId, retryCount);
}
use of org.ballerinalang.model.values.BFunctionPointer in project ballerina by ballerina-lang.
the class TransactionResourceManager method invokeCommittedFunction.
private void invokeCommittedFunction(int transactionBlockId) {
BFunctionPointer fp = committedFuncRegistry.get(transactionBlockId);
if (fp != null) {
BValue[] args = {};
BLangFunctions.invokeCallable(fp.value().getFunctionInfo(), args);
}
}
use of org.ballerinalang.model.values.BFunctionPointer in project ballerina by ballerina-lang.
the class BalScheduleAppointment method execute.
public void execute(Context ctx) {
FunctionRefCPEntry onTriggerFunctionRefCPEntry;
FunctionRefCPEntry onErrorFunctionRefCPEntry = null;
if (ctx.getLocalWorkerData().refRegs[0] != null && ctx.getLocalWorkerData().refRegs[0] instanceof BFunctionPointer) {
onTriggerFunctionRefCPEntry = ((BFunctionPointer) ctx.getRefArgument(0)).value();
} else {
ctx.setReturnValues(new BString(""), BLangVMErrors.createError(ctx, 0, "The onTrigger function is not provided"));
return;
}
if (ctx.getLocalWorkerData().refRegs[1] != null && ctx.getLocalWorkerData().refRegs[1] instanceof BFunctionPointer) {
onErrorFunctionRefCPEntry = ((BFunctionPointer) ctx.getRefArgument(1)).value();
}
String schedule = ctx.getStringArgument(0);
try {
Appointment appointment = new Appointment(this, ctx, schedule, onTriggerFunctionRefCPEntry, onErrorFunctionRefCPEntry);
ctx.setReturnValues(new BString(appointment.getId()));
} catch (SchedulingException e) {
ctx.setReturnValues(BLangVMErrors.createError(ctx, 0, e.getMessage()));
}
}
use of org.ballerinalang.model.values.BFunctionPointer in project ballerina by ballerina-lang.
the class BalScheduleTimer method execute.
public void execute(Context ctx) {
FunctionRefCPEntry onTriggerFunctionRefCPEntry;
FunctionRefCPEntry onErrorFunctionRefCPEntry = null;
if (ctx.getLocalWorkerData().refRegs[0] != null && ctx.getLocalWorkerData().refRegs[0] instanceof BFunctionPointer) {
onTriggerFunctionRefCPEntry = ((BFunctionPointer) ctx.getRefArgument(0)).value();
} else {
ctx.setReturnValues(new BString(""), BLangVMErrors.createError(ctx, 0, "The onTrigger function is not provided"));
return;
}
if (ctx.getLocalWorkerData().refRegs[1] != null && ctx.getLocalWorkerData().refRegs[1] instanceof BFunctionPointer) {
onErrorFunctionRefCPEntry = ((BFunctionPointer) ctx.getRefArgument(1)).value();
}
BStruct scheduler = (BStruct) ctx.getRefArgument(2);
long delay = scheduler.getIntField(0);
long interval = scheduler.getIntField(1);
try {
Timer timer = new Timer(this, ctx, delay, interval, onTriggerFunctionRefCPEntry, onErrorFunctionRefCPEntry);
ctx.setReturnValues(new BString(timer.getId()));
} catch (SchedulingException e) {
ctx.setReturnValues(BLangVMErrors.createError(ctx, 0, e.getMessage()));
}
}
Aggregations