2

I put Event Handler to query my events in axon framework in my spring application. I can put an event from my command up and I can read them from my query app.until reboot time no problem But when I reboot my query app Each time it reads same events again at the beginning and it cause duplicate process. How can I commit an event when I read it?

ToMyAxonConfig

@Bean
    public TokenStore tokenStore(Serializer serializer, EntityManagerProvider entityManagerProvider) {
      return JpaTokenStore.builder()
                .entityManagerProvider(entityManagerProvider)
                .serializer(serializer)
                .build();
    }

   @Autowired
    public void configureProcessors(EventProcessingConfigurer eventProcessingConfigurer) {
        TrackingEventProcessorConfiguration tepConfig = TrackingEventProcessorConfiguration.forSingleThreadedProcessing().andInitialTrackingToken(StreamableMessageSource::createHeadToken);
        eventProcessingConfigurer.registerTrackingEventProcessorConfiguration(config -> tepConfig);
    }
Bilgehan
  • 1,135
  • 1
  • 14
  • 41

1 Answers1

1

I have the feeling you haven't configured a TokenStore. In that case, Axon Framework will give you an InMemoryTokenStore implementation meaning that on every restart, you will get all Events again from your Event Store because your app does not have a 'state' for the token.

You can check the ref-guide to know more about it and also how to configure one properly from several options.

How can I commit an event when I read it?

Also to correct a bit the word usage here, Events are 'commited' on your Event Store. If you are talking about your Query Side App, you do not commit Events there but you just save a state based on Events.

Lucas Campos
  • 1,860
  • 11
  • 17
  • Commit mean is after restart I dont want to come same event again my eventhandler for that reason I said that.I try your suggestion thanks Lucas – Bilgehan Oct 11 '21 at 08:55
  • ,I add two different block to my code,then it stops reading same events after restart.Can you check my codes,do you have another suggestion or best bractice to do this or my addings are enough(you can check in first post) – Bilgehan Oct 12 '21 at 07:03
  • If you are using Spring Boot, only adding the spring-data-jpa dependency would do the trick and you won't have to configure anything. If you are not using Spring Boot, creating the JpaTokenStore the way you did is fine. The other configuration, about your processor is not needed and not related to the TokenStore. – Lucas Campos Oct 12 '21 at 08:37
  • I use spring boot ,but if dont put TrackingEventProcessorConfiguration tepConfig = TrackingEventProcessorConfiguration.forSingleThreadedProcessing().andInitialTrackingToken(StreamableMessageSource::createHeadToken); this part that time jpatokenstore is not enough in my case – Bilgehan Oct 12 '21 at 14:47
  • It should be, they are doing different things! One of them is making your token store persistent, meaning you won't lose track of what you already handled. The other is starting the token on the head of the stream, meaning that a new token will start from the head and not tail. Can you also share your pom and dependencies? – Lucas Campos Oct 12 '21 at 18:49
  • spring web starter,spring jpa starter 2.5.5 and other h2 dependecies and axon-spring-boot-starter 4.5.4 (I use axon and spring latest versions) thanks – Bilgehan Oct 13 '21 at 06:07