Yeah, the simple version of fix is this:

    private ThreadLocal<int[]> counter = new ThreadLocal<int[]>();

Filters don't have any complex lifecycle, so should be no problem to
init everything upfront.

On Thu, Jul 26, 2018 at 11:03 AM, D Tim Cummings <[email protected]> wrote:
> I see that 4.1.M2 has deprecated DataChannelFilter. Apparently we should use
> either DataChannelQueryFilter or DataChannelSyncFilter.
>
> I am following the example on
> https://cayenne.apache.org/docs/4.1/cayenne-guide/#lifecycle-events which
> still refers to DataChannelFilter. I would use DataChannelSyncFilter in this
> example but DataChannelSyncFilter does not have an init() method to override
> so would I initialise counter where it gets declared?
>
> Here is the code from the example.
>
> public class CommittedObjectCounter implements DataChannelFilter {
>
>     private ThreadLocal<int[]> counter;
>
>     @Override
>     public void init(DataChannel channel) {
>         counter = new ThreadLocal<int[]>();
>     }
>
>     @Override
>     public QueryResponse onQuery(ObjectContext originatingContext, Query
> query, DataChannelFilterChain filterChain) {
>         return filterChain.onQuery(originatingContext, query);
>     }
>
>     @Override
>     public GraphDiff onSync(ObjectContext originatingContext, GraphDiff
> changes, int syncType,
>             DataChannelFilterChain filterChain) {
>
>         // init the counter for the current commit
>         counter.set(new int[1]);
>
>         try {
>             return filterChain.onSync(originatingContext, changes,
> syncType);
>         } finally {
>
>             // process aggregated result and release the counter
>             System.out.println("Committed " + counter.get()[0] + "
> object(s)");
>             counter.set(null);
>         }
>     }
>
>     @PostPersist(entityAnnotations = Tag.class)
>     @PostUpdate(entityAnnotations = Tag.class)
>     @PostRemove(entityAnnotations = Tag.class)
>     void afterCommit(Persistent object) {
>         counter.get()[0]++;
>     }
> }
>
>
>
>
> Regards
>
> Tim



-- 
Best regards,
Nikita Timofeev

Reply via email to