스프링으로 데이터베이스 이벤트를 처리하고 싶습니다. 봄에는 이벤트 처리 메커니즘이 있으며 봄에 몇 가지 사용자 지정 이벤트 처리기를 정의했습니다. 예를 들어 직원이 시스템에서 비활성화되어 특정 활동 집합을 수행해야하는 경우 스프링 사용자 지정 이벤트 목록 작성기를 실행할 수있는 방법이 있습니다. 최대 절전 모드로 봄을 사용하고 있습니다. 이것을 달성하기 위해 최대 절전 모드가 될 수 있는지 의심하지만 봄을 처리하고 싶습니다. 미리 감사드립니다
내가 귀하의 질문을 올바르게 이해했다면, 최대 절전 모드 엔티티 변경 이벤트를 가로 채고 Spring에서 처리 할 수있는 이벤트를 트리거하는 방법을 알아야합니다. 항상 하이버 네이트에서 이벤트를 가로 챌 수 있습니다. 즉, 인터셉터를 사용하여 하이버 네이트 엔티티 요소의 속성 값을 검사 및 / 또는 변경할 수 있습니다. 예를 들어 Employee 엔터티에는 상태 (예 : String) 필드가 있으며,이 필드에 대한 변경 (예 : 활성에서 비활성으로 변경)을 감지하고 변경 사항이 데이터베이스에 유지되기 전에 몇 가지 작업을 수행 할 수 있습니다. 이 엔터티에있는 컬렉션에 대한 변경 사항도 가로 챌 수 있습니다. 간결함을 위해 필드 변경 만 수행합니다.
필드 변경 추적을위한 리스너 인터페이스 FieldChangeListener를 다음과 같이 정의하십시오.
public interface FieldChangeListener<T>{
/**
* Defining generically so that it can be implemented by any entity on which field change needs to be observed.
* @param propertyName - the property name on the entity
* @param entity - the entity object
* @param previousState - the old value
* @param currentState - the new value
* */
void onChange(Object[] previousState, Object[] currentState, String[] propertyName, Type[] types, Object entity);
}
인터셉터를 사용하려면 EmptyInterceptor 를 확장하는 클래스를 만들어야합니다 .
public class FieldChangeInterceptor extends EmptyInterceptor {
private Map<Class, FieldChangeListener<?>> listeners;
public void setListeners(Map<Class, FieldChangeListener<?>> listeners) {
this.listeners = listeners;
}
@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
FieldChangeListener<?> listener = listeners.get(entity.getClass());
boolean report = false;
// Only check for changes if an entity-specific listener was registered.
if (listener != null) {
listener.onChange(previousState, currentState, propertyNames, types, entity);
report = true;
}
return report;
}
}
컨텍스트 xml에서 인터셉터와 리스너를 다음과 같이 정의하십시오.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
...
</bean>
<bean id="fieldInterceptor" class="package.FieldChangeInterceptor">
<property name="listeners">
<util:map id="listeners" map-class="java.util.HashMap">
<entry>
<key><value>package.Employee</value></key><ref bean="employeeListener"/>
</entry>
</util:map>
</property>
</bean>
<bean id="employeeListener" class="package.listener.EmployeeListener"/>
EmployeeListener 클래스는 다음과 같습니다.
public class EmployeeListener implements FieldChangeListener<Employee>, ApplicationEventPublisherAware{
private ApplicationEventPublisher publisher;
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
@Override
public void onChange(Object[] previousState, Object[] currentState, String[] propertyName, Type[] types, Object obj) {
log.info("Enter onChange()...");
Employee employee = (Employee)obj;
boolean isChange = false;
for (int i = 0; i < currentState.length; i++) {
if (currentState[i] == null) {
if (previousState[i] != null){
isChange = true;
}else{
isChange = false;
}
} else if (!currentState[i].equals(previousState[i])) {
isChange = true;
}
if (isChange) {
//check for status property of Employee
if(propertyName[i].equals("status")){
//Do your activity here - maybe you want to publish application event using eventService.
//Define event.
publisher.publishEvent(event);
}
report = false;
}
}
log.info("Exit onChange()...");
}
사용자 지정 ApplicationEvent를 게시하려면 ApplicationEventPublisher에서 publishEvent () 메서드를 호출합니다. 이벤트 게시에 대한 자세한 내용은 여기에서 섹션 5.14.2 표준 및 사용자 지정 이벤트를 확인 하십시오 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다