How to get result of an Activity without waiting for ? In a event driven fashion - cadence-workflow

Team, I have a doubt in calling activities in parallel.
I know that activities can be called parallelly using the Async.function.
For my use case, I wanted to call n no of activities in parallel after receiving results from all activities I wanted to go to the next state either it could another activity or a decision task.
But here it gives me a Promise object which similar to Future in java. If we write promise.get() to get a result but it is blocking the thread.
I need something similar to event-driven fashion without blocking the thread.
Hope you understand my question!!

You don't have to use .get().
In Promise, you can register a callback function to be invoked when the result is available or an error is returned.
You can use one of the below depends on what you need:
thenApply
handle
thenCompose
exceptionally
This style is similar to Java's CompletableFuture.
/**
* Returns Promise that contains a result of a function. The function is called with the value of
* this Promise when it is ready. #completeExceptionally is propagated directly to the returned
* Promise skipping the function.
*
* <p>Note that no blocking calls are allowed inside of the function.
*/
<U> Promise<U> thenApply(Functions.Func1<? super V, ? extends U> fn);
/**
* Returns Promise that contains a result of a function. The function is called with the value of
* this Promise or with an exception when it is completed. If the function throws a {#link
* RuntimeException} it fails the resulting promise.
*
* <p>Note that no blocking calls are allowed inside of the function.
*/
<U> Promise<U> handle(Functions.Func2<? super V, RuntimeException, ? extends U> fn);
/**
* Returns a new Promise that, when this promise completes normally, is executed with this promise
* as the argument to the supplied function.
*
* #param fn the function returning a new Promise
* #param <U> the type of the returned CompletionStage's result
* #return the Promise that completes when fn returned Promise completes.
*/
<U> Promise<U> thenCompose(Functions.Func1<? super V, ? extends Promise<U>> fn);
/**
* Returns a new Promise that, when this promise completes exceptionally, is executed with this
* promise's exception as the argument to the supplied function. Otherwise, if this promise
* completes normally, then the returned promise also completes normally with the same value.
*
* #param fn the function to use to compute the value of the returned CompletionPromise if this
* CompletionPromise completed exceptionally
* #return the new Promise
*/
Promise<V> exceptionally(Functions.Func1<Throwable, ? extends V> fn);

Adding to the great Long's answer.
You can use Promise.anyOf to wait on the completion of a single Promise from a collection in a blocking manner.

Related

How to resolve 'The method observe(Viewer) is ambiguous for the type IViewerValueProperty<Viewer,Object>' compiler error

I was trying to use the org.eclipse.core.databinding plugin to bind my TableViewer input change.
When I tried to add the binding by below code:
1. TableViewer tableViewer = new TableViewer(parent);
2. IViewerObservableValue<Target> target = ViewerProperties.input(TableViewer.class).observe(tableViewer);
3. UpdateValueStrategy<String, Target> updateValueStrategy = new UpdateValueStrategy<>();
updateValueStrategy.setConverter(...);
4. this.bindingContext.bindValue(target, source, new UpdateValueStrategy<>(UpdateValueStrategy.POLICY_NEVER),
updateValueStrategy);
But, at line number 2, I'm getting an compiler error that 'The method observe(Viewer) is ambiguous for the type IViewerValueProperty<Viewer,Object>' compiler error.
When I look into the source of IViewerObservableValue, There are 2 methods similar, I tried type casting with Viewer or Object for tableViewer variable passed, but, I'm still getting the error.
`/**
* Returns an {#link IViewerObservableValue} observing this value property
* on the given viewer
*
* #param viewer
* the source viewer
* #return an observable value observing this value property on the given
* viewer
*/
public IViewerObservableValue<T> observe(Viewer viewer);
/**
* This method is redeclared to trigger ambiguous method errors that are hidden
* by a suspected Eclipse compiler bug 536911. By triggering the bug in this way
* clients avoid a change of behavior when the bug is fixed. When the bug is
* fixed this redeclaration should be removed.
*/
#Override
public IObservableValue<T> observe(S viewer);`
what I'm doing wrong?
Sorry Everyone, I have figured it out, We can do by following:
IViewerValueProperty<TableViewer, Target> target = ViewerProperties.<TableViewer, Target>input();
IObservableValue<Target> observe = target.observe(tableViewer);
I had forgot to add generic classes to the input() method call, which would have identified the specific observer(viewer) method.
Previously, as I had not provided the generics, the compiler was unable to distinguish between the observe(viewerType) and observe(S).
Thanks,
Pal

Glib - What is the most easy way to execute a GCallback directly?

What I currently do:
/* FIXME: Actually I just want to call that callback ... no idea how that can be done without using a signal */
/* So I just used some random unused signal for it */
gulong handler_id = g_signal_connect_swapped (window, "direction-changed", (GCallback) mycallback, window);
g_signal_emit_by_name (window,"direction-changed");
g_signal_handler_disconnect (window, handler_id);
Works well, but is an ugly hack.
I know it is somehow possible to call my callback by using g_closure_invoke() in some way, though so far I did not get that to work.
If it matters, the callback will be filled with a function which looks like that:
static void myCallbackMethod (GtkWindow *window);
A GCallback is just a function pointer of a certain type. You can call the function directly, just like any other C function call.
mycallback (window);

Does MongoCollection.forEach need to be thread safe?

When using the MongoDB Async Java Driver:
Does the following callback need to use a AtomicInteger counter or would a normal int do the job?
Block<Document> theBlock = new Block<Document>() {
AtomicInteger counter = new AtomicInteger();
#Override
public void apply(final Document document) {
counter.incrementAndGet();
}
};
SingleResultCallback<Void> callbackWhenFinished = ...
collection.find().forEach(theBlock, callbackWhenFinished);
The only real difference between the MongoDB Java API and its async counterpart is that the methods of the latter are non-blocking and take callbacks as arguments. This means that what you receive in your callback is equivalent to what the method returns in the non-async API.
Here, you use the find method. It returns a "normal" iterable, so calling forEach on it will not result in multiple threads.
In other words, you don't need an AtomicInteger: your apply method is called sequentially, by the same thread.
If you still have doubts or need a "proof", you can do one of the following:
add a System.out.println(Thread.currentThread().getName()); inside your block. You will see it is always performed by the same thread;
add a breakpoint inside your block, configured to stop only the thread. Once again, the breakpoint will block the whole code.

Is there an operator or provision in RxJS to call every time a function is called?

I have a function className.doSomething() which returns an observable.I want to know the time when this function was called or subscribed.
className.doSomething()
:callAtSubscribe( "print current time")
:tap()
:subscribe()
Need something appropriate in place of callAtSubscribe.
You can wrap the method in an Observable.Create. This will allow you to track when the subscription is made. If you then wrapped that in a method, you could track when the method was called. This would allow you to see when the method was called (but potentially not subscribed to the result).
/**
* Module dependencies.
*/
var Rx = require('rx');
Rx.Observable.prototype.log = function (logger, sourceName) {
var source = this;
logger.debug({source : sourceName, kind : 'Created'});
return Rx.Observable.create(function(observer) {
logger.debug({source : sourceName, kind : 'Subscribe'});
return source.subscribe(observer);
});
};
This is a very cut down version of a log method I use. It can be heavily modified to include timings, wiretap of values, error, completions and terminations.

What do you pass to a method wait(int*)

When I simply pass the int I get the warning:
WARNING: Incompatible integer to pointer conversion passing int to parameter of
type int *
In another words what is int *
This requires the address of an integer variable, not the integer variable itself. You can get the address of a variable with the & operator. So the following code would work:
int i = 10;
wait( &i );
The man page for wait() describes what the argument is for:
pid_t
wait(int *stat_loc);
The wait() function suspends execution of its calling process until stat_loc information is available for a terminated child process, or a signal is received. On return from a successful wait() call, the stat_loc area contains termination information about the process that exited as defined below.
The * indicates that a pointer is desired. Since arguments are passed by value to function calls, the way a value is returned via a function parameter is through a pointer to the object receiving the value.
int status;
pid_t p;
p = wait(&status);