why @EventHandler method running every time I start my application?

I just try to use Axon 4.2, and feel very confused about some place.

1.first problem, when I start my application, it logs like this:

`

[user-center:172.28.1.97:10040] 2019-08-22 22:30:14.868 WARN 24132 [EventProcessor[queryModel]-0] org.axonframework.eventhandling.TrackingEventProcessor Fetch Segments for Processor ‘queryModel’ failed: Could not load segments for processor [queryModel]. Preparing for retry in 4s
[user-center:172.28.1.97:10040] 2019-08-22 22:30:21.988 WARN 24132 [EventProcessor[queryModel]-0] org.axonframework.eventhandling.TrackingEventProcessor Fetch Segments for Processor ‘queryModel’ failed: Could not load segments for processor [queryModel]. Preparing for retry in 8s

`

to solve this problem, I tries 2 ways:

one is switch to jpa, but another error happens:

`

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: SagaEntry is not mapped [SELECT new org.axonframework.modelling.saga.repository.jpa.SerializedSaga(se.serializedSaga, se.sagaType, se.revision) FROM SagaEntry se WHERE se.sagaId = :sagaId]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:670)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350)
at com.sun.proxy.$Proxy231.createQuery(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:305)
at com.sun.proxy.$Proxy231.createQuery(Unknown Source)

`

don’t know how to solve this, so I tried the second way, add this:

@ComponentScan(basePackages = {"org.axonframework"})

then my application start success.

but another thing happens , witch really confused me.
after creating some events, every time I start my application, my @EventHandler method runs,

`

@AllowReplay(false)
@EventHandler
public void on(SysUserModifyBaseInfoEvent event, ReplayStatus replayStatus) {
    SysUser sysUser = defaultMappper.map(event.getSysUserAO(), SysUser.class);
    userService.updateById(sysUser);
}

`

I thought it was a event replay, so I add @AllowReplay(false), but it wasn’t…

can you tell my the reason? and how to solve this problems? thank you very much~~

I debug the code, and find that , every time I restart my application, it run this code:

@Override
public void start() {
    processorInfoSource.get().start();
    eventProcessorControlService.get().start();
}
logger.info("open stream: {}", nextToken);

StreamObserver<GetEventsRequest> requestStream = eventStoreClient
        .listEvents(context, new StreamObserver<EventWithToken>() {
            @Override
            public void onNext(EventWithToken eventWithToken) {
                logger.debug("Received event with token: {}", eventWithToken.getToken());
                consumer.push(eventWithToken);
            }

            @Override
            public void onError(Throwable throwable) {
                consumer.fail(new EventStoreException(
                        "Error while reading events from the server", throwable
                ));
            }

            @Override
            public void onCompleted() {
                consumer.fail(new EventStoreException("Error while reading events from the server",
                                                      new RuntimeException("Connection closed by server")));
            }
        });

gets all the events from Axon Server ,and process them. But should’t this kind of events be Replay type?

在 2019年8月22日星期四 UTC+8下午10:46:30,Andrew Yang写道:

Hi

If your application has EventHandlers (configured to be used with Tracking Event Processors = default), they try to restore the full event stream.
A good idea is to store the Tracking token in the same data store as your projection data. I’ll give you some examples:

  • imagine your event handlers collect events and build an In-Memory map with values collected. In this case an In-Memory Token store is a good idea - on every restart you will replay all events and re-create the projection
  • imagine you use JPA and write projection data into a RDBMS - then you should use a JPA Token store.
  • imagine you use Mongo to store events as JSON in MongoDB - for this case a Mongo Token Store exists.

The idea is that tracking processors have a tracking token controlling where they are in the event stream. If the value there differs from the value of the last event in the event store, your processor will get the stream until it is up to the head of it.
You should reason about persisting this information over restart and can control whether you get the event stream delivered every time or not.

Hope this helps,

Cheers,

Simon

Hello Simon!

Thank you for the explanation!

Your answer really helps me. And finally I solved this problem. Thank you very much!

Andrew Yang