vendredi 9 avril 2021

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

I have an interface in a common maven project, there are two publish methods both returning boolean:

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

I have one implementation of this interface:

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) {
    }
}

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 instantiates a Spring Bean of the Publisher class:

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

The client app is using spring batch, it publishes like this in workflow config class:

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

where publisher is IPublisher instance. The items are processed in chunks and at the end of processing I need a summary count of how many published but I am unsure how to achieve this since publish() returns boolean. I want to get total publish count then I can hold it in executionContext. so that I can use this count for reporting in subsequent step of my workflow. e.g. received vs published count. How can I achieve this?

Aucun commentaire:

Enregistrer un commentaire