简体   繁体   English

Quartz Job中的RequestedScope

[英]RequestedScope in Quartz Job

Well, i'm trying to use RequestedScoped beans inside a Quartz Job, see: 好吧,我正在尝试在Quartz Job中使用RequestedScoped bean,请参见:

public class JobRoboFtp implements Job {

    @Inject
    private AcervoVideoService acervoVideoService;

    @Inject
    private ConfiguracaoService configuracaoService;

    @Inject
    private FtpManager ftpManager;

But i got always: 但是我总是:

No active contexts for scope type javax.enterprise.context.RequestScoped

Well, If i change this Services to @Dependent scope everything works but i would like to use RequestedScope. 好吧,如果我将此服务更改为@Dependent范围,则一切正常,但我想使用RequestedScope。 There is any way ? 有什么办法吗?

Since you didn't say much about versions, I am going to assume some of the latest versions of Weld 2.x (or even 3.x) - then there is a way. 由于您对版本的说法不多,因此我将假设一些最新版本的Weld 2.x(甚至是3.x)-这样就可以了。 I also assume you are talking about SE environment, as otherwise request scope would auto-activate during requests. 我还假设您正在谈论SE环境,否则请求范围将在请求期间自动激活。

If we are talking CDI 1.2 (Weld 2.x) then you need to add explicit dependency on Weld API and make use of it. 如果我们正在谈论CDI 1.2(Weld 2.x),则需要添加对Weld API的显式依赖项并加以利用。 The dependency is org.jboss.weld:weld-api and the functionality you are looking for is @ActivateRequestContext interceptor binding . 依赖项是org.jboss.weld:weld-api ,而您正在寻找的功能是@ActivateRequestContext拦截器binding If you are looking for a link to Weld docs, its here - note that this was added in Weld 2.4! 如果您要在此处找到 Weld文档的链接,请注意-这是Weld 2.4中添加的!

The principle is simple - it intercepts method calls and activated context when entering the method, then destroys it when you exit the method. 原理很简单-它在进入方法时拦截方法调用和激活的上下文,然后在退出方法时销毁它。

@ActivateRequestContext
public void myMethod() {
  // any content in here will have request context ACTIVE
  doAwesomeThings();
}

If we are talking about CDI 2.0/Weld 3.x - then the very same approach was adapted by CDI (Weld version works there as well). 如果我们谈论的是CDI 2.0 / Weld 3.x,那么CDI会采用相同的方法(Weld版本也可以使用)。 You can read about it here . 你可以在这里阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM