跟踪发布到事件中心并存储在执行上下文中的项目的计数

track count of item published to event hub and store in execution context

我在一个普通的maven项目中有一个接口,有两个发布方法都返回布尔值:

public interface IPublisher<T, U> {    
    boolean publish(Context ctx, T model);    
    boolean publish(Context ctx, List<? extends ModelHolder> model);    
}

我有一个这个接口的实现:

public class Publisher implements IPublisher<PriceHolder, JSONObject> {

    private final DataPublisher dataPublisher;

    public Publisher(final DataPublisher dataPublisher) {
        this.dataPublisher = dataPublisher;
    }

    @Override
    public boolean publish(Context context, PriceHolder priceHolder) {
    
        if (isInvalid(priceHolder)) {
            return false;
        }

        try {
            dataPublisher.publish(data);

        } catch (ConnectionException | DataBuilderException ex) {
            String message = "someMessage";
            LOGGER.error(message, ex);
        } catch (TimeoutException e) {
            LOGGER.error(message, e);
        }

        return true;
    }

    @Override
    public boolean publish(Context ctx, List<? extends ModelHolder> list) {
        if (list == null) {
            return false;
        }

        for (ModelHolder item : list) {
            publish(ctx, (PriceHolder) item);
        }

        return true;
    }

    private boolean isInvalid(PriceHolder priceHolder) {
    }
}

目前,此实现不记录已发布的数量,这是我需要传递给调用 publish() 的客户端的信息。

客户端实例化一个 Spring 发布者的 Bean class:

@Bean
public IPublisher publisher(DataItemPublisher dataItemPublisher) {
    return new Publisher(dataItemPublisher);
}

客户端应用程序正在使用 spring 批处理,它在工作流配置中像这样发布 class:

@Bean
@Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
public ItemWriter<PriceHolder> publish(@Value("#{jobExecutionContext['MY_CONTEXT']}") Context ctx) {
    return items -> {
        publisher.publish(ctx, items);
    };
}

其中 publisher 是 IPublisher 实例。这些项目以块的形式处理,在处理结束时,我需要对已发布的数量进行汇总计数,但我不确定如何实现这一点,因为 publish() returns 布尔值。我想获得总发布计数,然后我可以将它保存在 executionContext 中。这样我就可以在工作流程的后续步骤中使用此计数进行报告。例如收到与发布的数量。我怎样才能做到这一点?

Currently, this implementation does not not record how many have been published which is what I need to pass to the client who is calling publish().

由于此实现没有为客户端提供了解发布了多少项目的方法,并且由于您说过 The client app is using spring batch,您将无法获取该信息并将其存储在执行上下文中Spring 批次。您需要修复接口并使其 return 成为比布尔值更丰富的类型。