static class GreetingActivitiesImpl implements GreetingActivities {
@Override
public String composeGreeting(String greeting, String name) {
return greeting + " " + name + "!";
}
}
static class GreetingActivitiesImpl2 implements GreetingActivities2 {
@Override
public String composeGreeting(String greeting, String name) {
System.out.println("Activity 2");
return "Some Value!!!";
}
}
worker.registerActivitiesImplementations(new GreetingActivitiesImpl(), new GreetingActivitiesImpl2());
while I am registering the activities in worker I am getting below exception.
Exception in thread "main" java.lang.IllegalArgumentException: "ComposeGreeting" activity type is already registered with the worker
at io.temporal.internal.sync.POJOActivityTaskHandler.registerActivityImplementation(POJOActivityTaskHandler.java:116)
at io.temporal.internal.sync.POJOActivityTaskHandler.registerActivityImplementations(POJOActivityTaskHandler.java:179)
at io.temporal.internal.sync.SyncActivityWorker.registerActivityImplementations(SyncActivityWorker.java:55)
at io.temporal.worker.Worker.registerActivitiesImplementations(Worker.java:326)
at io.temporal.samples.hello.HelloActivity2.main(HelloActivity2.java:195)
Hi @anil_kumble
Activity types are by default registered with their method name (with first character upper-cased).
You are getting this error because you are registering two Activity types with the same name, namely “ComposeGreeting” corresponding to your two “composeGreeting” activity methods in the two Activity interfaces.
You have a couple of options:
Rename one of your “composeGreeting” methods (keeping them unique) in your Activity interface and its corresponding impl.
Each of the Activity impls you register with your Worker will need its own interface, but you can use interface inheritance for common methods, for example:
you mean that each activity interface will have a single implementation. I think it is violating the basic principle of having an interface. A need for an interface will arrive If we require multiple implementations of it. If it is single then we could have it just as a class right instead of having an interface and implementing it?
I don’t know how to use it. Please give some examples?
A small doubt that can we have different activity options for the methods kept in the activity interface. As of now, I can see activity options are specified to the whole interface.
you mean that each activity interface will have a single implementation. I think it is violating the basic principle of having an interface. A need for an interface will arrive If we require multiple implementations of it. If it is single then we could have it just as a class right instead of having an interface and implementing it?
It has multiple implementations. One is actual activity implementation, another is the activity stub which is used to invoke an activity from the workflow code.
A small doubt that can we have different activity options for the methods kept in the activity interface. As of now, I can see activity options are specified to the whole interface.
It is possible to specify activity options per activity type through WorkflowImplementationOptions.setActivityOptions and Workflow.newActivityStub. But these features are not released yet.
Here, it is done at compile time. I what this needs to be accomplished at run time via programmatically.
I didn’t see any other options other than this in the doc.
Could you explain what exactly are you trying to achieve by “what this needs to be accomplished at run time via programmatically.”. Are you planning to implement activities dynamically?
No it doesn’t have multiple implementations,
If you see the example HelloActivity where we have single activity interface called GreetingActivities and it has only one implementation called GreetingActivitiesImp.
private final GreetingActivities activities =
Workflow.newActivityStub(
GreetingActivities.class,
ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(30)).build());
The above code is not an implementation of an activity interface, it just a instantiation.
Oh… we have dynamic activity as a separate feature. I didn’t notice it in the temporal doc. Could you please share me the doc. And also sample java example for that
And one more doubt that, since it is restricted to single implementation of a ActivityInterface, Why it is designed to create an interface and implement it. Instead we could have it as a class where we can annotate the class with ActivityInterface, right?
The basic idea of having an interface is to have multiple variety of implementations of the method right ? But here it is a single implementation of an interface, That’s why still I’m confusing
Workflow code needs to know only the activity interface to invoke it. It doesn’t need to know the activity implementation which can be running in a different process (or even be implemented in a different language like Go).
So the interface was used to specify the signature of an activity without a specific implementation details.
Okay, let me go through it, Also if any other new feature not listed in the doc kindly just add the Java Doc link end of the doc so that it will not be missed.
An implementation of DynamicActivity should not use @ActivityInterface annotation.
And it can be invoked using any other interface annotated with @ActivityInterface or using an untyped stub created through Workflow.newUntypedActivityStub.