8.12.3. Jakarta Interceptors를 사용하여 Jakarta Enterprise Bean-clustered 타이머 새로 고침

새로 고침 간격이 만료되기 전에 타이머를 새로 고치도록 비즈니스 방법에 Jakarta Interceptor를 설정하여 타이머를 프로그래밍 방식으로 새로 고칠 수 있습니다.

참고

클러스터형 배포에서는 여러 노드가 짧은 간격으로 데이터 저장소를 업데이트하면 메모리 내 타이머 상태가 일시적으로 동기화되지 않을 수 있습니다.

사전 요구 사항

  • 데이터베이스 지원 클러스터형 Jakarta Enterprise Bean을 구성했습니다.

절차

  1. wildfly.ejb.timer.refresh.enabledtrue 로 활성화하는 Jakarta Interceptors를 구현합니다.

    import javax.interceptor.AroundInvoke;
    import javax.interceptor.Interceptor;
    import javax.interceptor.InvocationContext;
    
    /**
     * An interceptor to enable programmatic timer refresh across multiple nodes.
     */
    @Interceptor
    public class RefreshInterceptor {
        @AroundInvoke
        public Object intercept(InvocationContext context) throws Exception {
            context.getContextData().put("wildfly.ejb.timer.refresh.enabled", Boolean.TRUE);
            return context.proceed();
        }
    }
  2. 자카르타 인터셉터 구성.

    • 대상 상태 비저장 또는 Singleton 빈 비즈니스 방법으로 자카르타 인터셉터를 구성할 수 있습니다. When wildfly.ejb.timer.refresh.enabledtrue 로 설정되어 타이머를 반환하기 전에 TimerService.getAllTimers() 를 호출하여 타이머 데이터 저장소를 새로 고칩니다.

      @Singleton
      public class RefreshBean1 ... {
      
          @Interceptors(RefreshInterceptor.class)
          public void businessMethod1() {
              ...
              // since wildfly.ejb.timer.refresh.enabled is set to true in interceptor for this business method,
              // calling timerService.getAllTimers() will first refresh from timer datastore before returning timers.
              final Collection<Timer> allTimers = timerService.getAllTimers();
              ...
          }
      }
    • 또는 전용 비즈니스 방법을 구현하여 필요한 경우 애플리케이션의 다른 부분이 호출하는 타이머를 프로그래밍 방식으로 새로 고칠 수 있습니다.

          @Interceptors(RefreshInterceptor.class)
          public List<Timer> getAllTimerInfoWithRefresh() {
              return timerService.getAllTimers();
          }
      
          public void businessMethod1() {
              final LocalBusinessInterface businessObject = sessionContext.getBusinessObject(LocalBusinessInterface.class);
              businessObject.getAllTimerInfoWithRefresh();
      
              // timer has been programmatically refreshed from datastore.
              // continue with other business logic...
          }