alibaba / SmartEngine

SmartEngine is a lightweight business orchestration engine.
Apache License 2.0
768 stars 219 forks source link

custom 模式和 database 模式可以同时存在吗?应该怎么配置? #82

Closed 1725636955 closed 2 years ago

1725636955 commented 2 years ago

目前引入两个包,就会冲突报错,找到了两个 ExecutionInstanceStorage

Failed to instantiate [com.alibaba.smart.framework.engine.SmartEngine]: Factory method 'constructSmartEngine' threw exception; nested exception is com.alibaba.smart.framework.engine.exception.EngineException: Duplicated bindKeyClass found interface com.alibaba.smart.framework.engine.instance.storage.ExecutionInstanceStorage for group common, because of duplicated annotation or init twice.

1725636955 commented 2 years ago

`@Order @Configuration @ConditionalOnClass(SmartEngine.class) @Slf4j public class SmartEngineAutoConfiguration implements ApplicationContextAware {

private ApplicationContext applicationContext;

@Bean
@ConditionalOnMissingBean
public SmartEngine constructSmartEngine() {
    ProcessEngineConfiguration processEngineConfiguration = new DefaultProcessEngineConfiguration();

// 自定义处理类获取 processEngineConfiguration.setInstanceAccessor(new CustomInstanceAccessService());

// see https://github.com/alibaba/SmartEngine/wiki/SmartEngine-UserGuide--Chinese-Version-(%E4%B8%AD%E6%96%87%E7%89%88)#usertask // UserTask是DataBase模式下特有的功能。 // UserTask和TaskAssigneeDispatcher,待办列表(TaskInstance)紧密关联。 通过TaskAssigneeDispatcher,可以实现自动获取某个节点的任务处理者 processEngineConfiguration.setTaskAssigneeDispatcher(new DefaultTaskAssigneeDispatcher());

// 自定义 processInstanceId 生成 processEngineConfiguration.setIdGenerator(new CustomerIdGenerator());

// 自定义锁 processEngineConfiguration.setLockStrategy(new DefaultLockStrategy());

// SimpleAnnotationScanner scanner = new SimpleAnnotationScanner("com.alibaba.smart.framework.engine.persister.database", // "com.alibaba.smart.framework.engine.service"); // processEngineConfiguration.setAnnotationScanner(scanner);

    SmartEngine smartEngine = new DefaultSmartEngine();
    smartEngine.init(processEngineConfiguration);

    deployProcessDefinition(smartEngine);

    return smartEngine;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
}

private class CustomInstanceAccessService implements InstanceAccessor {
    @Override
    public Object access(String name) {
        try {
            return applicationContext.getBean(name);
        } catch (BeansException e) {
            log.error("smart engine 获取 bean 异常", e);
            throw e;
        }
    }

}

private void deployProcessDefinition(SmartEngine smartEngine) {
    RepositoryCommandService repositoryCommandService = smartEngine
            .getRepositoryCommandService();

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    try {
        Resource[] resources = resolver.getResources("smart-engine/*.xml");
        for (Resource resource : resources) {
            InputStream inputStream = resource.getInputStream();
            repositoryCommandService.deploy(inputStream);
            IOUtil.closeQuietly(inputStream);
        }
    } catch (Exception e) {
        throw new EngineException(e);
    }

}

/**
 * 自定义 processInstanceId 生成
 */
private static class CustomerIdGenerator implements IdGenerator {

    private static final Sequence SEQUENCE = new Sequence(null);

    @Override
    public void generate(Instance instance) {
        instance.setInstanceId(String.valueOf(SEQUENCE.nextId()));
    }
}

}`

1725636955 commented 2 years ago

这是我的配置

1725636955 commented 2 years ago

我理解应该是需要自定义 SimpleAnnotationScanner 的扫描路径,但是不知道需要哪些

vavi commented 2 years ago

目前不能同时在一个应用内部署2种模式

1725636955 commented 2 years ago

目前不能同时在一个应用内部署2种模式

ok