Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/isolate-listener-registration #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sipios.spring.data.event.broadcaster.DataEventBroadcaster;
import com.sipios.spring.data.event.listener.DataEventListener;
import com.sipios.spring.data.event.listener.DataEventListenerRegistration;
import org.springframework.context.annotation.Import;

import java.lang.annotation.Documented;
Expand All @@ -11,10 +12,9 @@

import static java.lang.annotation.RetentionPolicy.RUNTIME;


@Target(ElementType.TYPE)
@Retention(RUNTIME)
@Documented
@Import({DataEventBroadcaster.class, DataEventListener.class})
@Import({DataEventBroadcaster.class, DataEventListener.class, DataEventListenerRegistration.class})
public @interface EnableDataEvent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,17 @@

import com.sipios.spring.data.event.annotation.DataEventEntity;
import com.sipios.spring.data.event.broadcaster.DataEventBroadcaster;
import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManagerFactory;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.*;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.persister.entity.EntityPersister;
import org.springframework.stereotype.Component;

@Component
public class DataEventListener implements PostInsertEventListener, PostUpdateEventListener, PostDeleteEventListener {

private final DataEventBroadcaster dataEventBroadcaster;
private final EntityManagerFactory entityManagerFactory;

@PostConstruct
private void init() {
SessionFactoryImpl sessionFactory = entityManagerFactory.unwrap(SessionFactoryImpl.class);
EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
registry.getEventListenerGroup(EventType.POST_INSERT).appendListener(this);
registry.getEventListenerGroup(EventType.POST_UPDATE).appendListener(this);
registry.getEventListenerGroup(EventType.POST_DELETE).appendListener(this);
}

public DataEventListener(DataEventBroadcaster dataEventBroadcaster, EntityManagerFactory entityManagerFactory) {
public DataEventListener(DataEventBroadcaster dataEventBroadcaster) {
this.dataEventBroadcaster = dataEventBroadcaster;
this.entityManagerFactory = entityManagerFactory;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.sipios.spring.data.event.listener;

import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManagerFactory;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.internal.SessionFactoryImpl;
import org.springframework.stereotype.Component;

@Component
public class DataEventListenerRegistration {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tu as trouvé le moyen de tester la méthode init ? Sinon on peut peut-être exclure cette classe du coverage ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est compliqué car pour tester il faut avoir un EntityManagerFactory dans le application context et je galère 🙂

Mais j'y arriverai !


private final EntityManagerFactory entityManagerFactory;
private final DataEventListener dataEventListener;

public DataEventListenerRegistration(EntityManagerFactory entityManagerFactory, DataEventListener dataEventListener) {
this.entityManagerFactory = entityManagerFactory;
this.dataEventListener = dataEventListener;
}

@PostConstruct
private void init() {
SessionFactoryImpl sessionFactory = entityManagerFactory.unwrap(SessionFactoryImpl.class);
EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
registry.getEventListenerGroup(EventType.POST_INSERT).appendListener(dataEventListener);
registry.getEventListenerGroup(EventType.POST_UPDATE).appendListener(dataEventListener);
registry.getEventListenerGroup(EventType.POST_DELETE).appendListener(dataEventListener);
}
}
Loading