2017-09-08 25 views
2

은 내가 스레드를 실행하려고하면이 오류가 있습니다실행 과정 (JSF 2.2 - WELD 2.3)

WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped 

나는이 시도 : No active contexts for scope type javax.enterprise.context.RequestScoped when invoking a bean from a thread

를 그래서 나는이 오류가 :

2017-09-08 14:09:56,205 ERROR [br.com.ideallsistemas.obrigacoes.util.jsf.JsfExceptionHandler] Erro: #{emailCadastroBean.enviarEmail()}: java.lang.NoClassDefFoundError: org/jboss/weld/environment/se/Weldjavax.faces.FacesException: #{emailCadastroBean.enviarEmail()}: java.lang.NoClassDefFoundError: org/jboss/weld/environment/se/Weld 

빈에서 호출 내 방법

public void enviarEmail(Email email) { 
    List<String> destinatarios = destinatariosOf(email); 
    Weld weld = new Weld(); 
    final WeldContainer container = weld.initialize(); 
    RequestContext requestContext= container.instance().select(RequestContext.class, UnboundLiteral.INSTANCE).get(); 
     requestContext.activate(); 

    final EnvioEmailService pojo = container.instance().select(EnvioEmailService.class).get(); 
    Thread envioiEmail = new Thread() { 
     public void run() { 
      try {           
       for (String destinatario : destinatarios) { 
        pojo.envioEmailTaskDo(email, destinatario); 
       } 
      }catch (Exception e) { 
       System.out.println(e.getMessage()); 
      } 
     } 
    }; 
    envioiEmail.start(); 
    weld.shutdown(); 
} 
,536,

답변

1

JEE 응용 프로그램의 경우 스레드를 직접 만들지 마십시오!

@Asynchronous 주석을 사용해야합니다. 이 같은 EJB에서

사용 :

@Stateless 
public class NameOfEJB { 

    @Asynchronous 
    public void sendEmail() { 
     //send email here 
    } 
} 

를 주입 및 기타 구성 요소에서 사용은 (JSF는 콩, CDI 콩, 다른 EJB를 ... 관리)

@Named 
@ViewScoped 
public class CDIBean implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @EJB 
    private NameOfEJB nameOfEJB; 

    public void processForm() { 
     nameOfEJB.sendEmail(); 
    } 

} 

컨테이너가 할 것 비동기 프로세스.

코드가 더 간단하고 깨끗하며 유지 관리 가능하다는 점에 유의하십시오.