This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use spring events to initialize workers and added tests
1. Add annotation support for pollingInterval 2. Allow configuration using spring 3. Tests
- Loading branch information
Showing
16 changed files
with
535 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...g/src/main/java/com/netflix/conductor/client/spring/ConductorWorkerAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2023 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.netflix.conductor.client.spring; | ||
|
||
import java.util.Map; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.event.ContextRefreshedEvent; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.netflix.conductor.client.http.TaskClient; | ||
import com.netflix.conductor.sdk.workflow.executor.task.AnnotatedWorkerExecutor; | ||
import com.netflix.conductor.sdk.workflow.executor.task.WorkerConfiguration; | ||
|
||
@Component | ||
public class ConductorWorkerAutoConfiguration | ||
implements ApplicationListener<ContextRefreshedEvent> { | ||
|
||
@Autowired private TaskClient taskClient; | ||
|
||
@Override | ||
public void onApplicationEvent(ContextRefreshedEvent refreshedEvent) { | ||
ApplicationContext applicationContext = refreshedEvent.getApplicationContext(); | ||
Environment environment = applicationContext.getEnvironment(); | ||
WorkerConfiguration configuration = new SpringWorkerConfiguration(environment); | ||
AnnotatedWorkerExecutor annotatedWorkerExecutor = | ||
new AnnotatedWorkerExecutor(taskClient, configuration); | ||
|
||
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Component.class); | ||
beans.values() | ||
.forEach( | ||
bean -> { | ||
annotatedWorkerExecutor.addBean(bean); | ||
}); | ||
annotatedWorkerExecutor.startPolling(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...t-spring/src/main/java/com/netflix/conductor/client/spring/SpringWorkerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2023 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.netflix.conductor.client.spring; | ||
|
||
import org.springframework.core.env.Environment; | ||
|
||
import com.netflix.conductor.sdk.workflow.executor.task.WorkerConfiguration; | ||
|
||
public class SpringWorkerConfiguration extends WorkerConfiguration { | ||
|
||
private final Environment environment; | ||
|
||
public SpringWorkerConfiguration(Environment environment) { | ||
this.environment = environment; | ||
} | ||
|
||
@Override | ||
public int getPollingInterval(String taskName) { | ||
String key = "conductor.worker." + taskName + ".pollingInterval"; | ||
return environment.getProperty(key, Integer.class, 0); | ||
} | ||
|
||
@Override | ||
public int getThreadCount(String taskName) { | ||
String key = "conductor.worker." + taskName + ".threadCount"; | ||
return environment.getProperty(key, Integer.class, 0); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
client-spring/src/test/java/com/netflix/conductor/client/spring/Workers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2023 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.netflix.conductor.client.spring; | ||
|
||
import java.util.Date; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.netflix.conductor.sdk.workflow.executor.task.TaskContext; | ||
import com.netflix.conductor.sdk.workflow.task.InputParam; | ||
import com.netflix.conductor.sdk.workflow.task.WorkerTask; | ||
|
||
@Component | ||
public class Workers { | ||
|
||
@WorkerTask(value = "hello", threadCount = 3) | ||
public String helloWorld(@InputParam("name") String name) { | ||
TaskContext context = TaskContext.get(); | ||
System.out.println(new Date() + ":: Poll count: " + context.getPollCount()); | ||
if (context.getPollCount() < 5) { | ||
context.addLog("Not ready yet, poll count is only " + context.getPollCount()); | ||
context.setCallbackAfter(1); | ||
} | ||
|
||
return "Hello, " + name; | ||
} | ||
|
||
@WorkerTask(value = "hello_again", pollingInterval = 333) | ||
public String helloAgain(@InputParam("name") String name) { | ||
TaskContext context = TaskContext.get(); | ||
System.out.println(new Date() + ":: Poll count: " + context.getPollCount()); | ||
if (context.getPollCount() < 5) { | ||
context.addLog("Not ready yet, poll count is only " + context.getPollCount()); | ||
context.setCallbackAfter(1); | ||
} | ||
|
||
return "Hello (again), " + name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
conductor.client.rootUri=http://localhost:8080/api/ | ||
conductor.worker.hello.threadCount=100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.