简体   繁体   English

如何将Jersey豆注入Weld的Quartz作业中?

[英]How do I inject Jersey beans into Quartz job in Weld?

I have a pure JAX-RS application both in Glassfish and in jetty-servlet. 我在Glassfish和jetty-servlet中都有一个纯JAX-RS应用程序。 I'm trying to inject some beans into Quartz job. 我正在尝试将一些豆类注入Quartz作业。

I've annotated Job fields with @javax.inject.Inject , but I also believe I need to make Job factory available to container, or vice versa - I'm somewhat lost here what end to start with. 我已经用@javax.inject.Inject注释了Job字段,但我也相信我需要使Job工厂对容器可用,反之亦然-我在这里开始时有些迷惑。

How do I accomplish it? 我该如何完成?

Hi Victor without some more detail regarding your application I am going to assume you're running in a servlet container. Victor,您好,而您的应用程序没有更多详细信息,我将假设您正在servlet容器中运行。 There is no way to do it using a "pure" JAX-RS application. 使用“纯” JAX-RS应用程序无法做到这一点。

However I would recommend you use CDI (Weld or OpenWebBeans). 但是,我建议您使用CDI(Weld或OpenWebBeans)。 You can then use Deltaspike to enable the request context inside your Quartz job get a reference to a request scoped bean. 然后,您可以使用Deltaspike在Quartz作业中启用请求上下文,以获取对请求范围的bean的引用。

Enabling Weld in your servlet container is quite simple 在servlet容器中启用Weld很简单

A guide on enabling the request context outside of an HTTP request is available here 此处提供有关 HTTP请求之外启用请求上下文的指南。

By default you cannot inject @RequestScoped beans into your quartz job (there is no HTTP request hence no request context). 默认情况下,您不能将@RequestScoped bean注入您的石英作业(没有HTTP请求,因此没有请求上下文)。 To work around this you can either enable the request context by following the link above OR (and probably my approach) inject @Dependent scoped beans (which do the work) into your JAX RS beans (essentially wrappers), you can then easily get references to the @Dependent scoped beans inside your quartz job. 要解决此问题,您可以通过单击上面的链接来启用请求上下文,或者(可能是我的方法)将@Dependent范围内的bean(完成工作)注入到JAX RS bean(基本上是包装器)中,然后可以轻松地获取引用到石英作业中的@Dependent作用域豆。

Here is my web.xml and pom.xml for running Weld and Jersey inside jetty, you will need jetty-plus, jetty-jndi and Weld dependencies. 这是我在jetty中运行Weld和Jersey的web.xmlpom.xml ,您将需要jetty-plus,jetty-jndi和Weld依赖项。

Here is some info about getting JNDI setup within jetty, however I do not use this method as I start jetty as an embedded container within a SE application, here is a code snippet of what I do: 以下是有关在jetty中设置JNDI设置的一些信息 ,但是,当我将jetty作为SE应用程序中的嵌入式容器启动时,我不使用此方法,这是我的工作的代码段:

String[] configurationClasses =
{
    "org.eclipse.jetty.webapp.WebInfConfiguration",
    "org.eclipse.jetty.webapp.WebXmlConfiguration",
    "org.eclipse.jetty.webapp.MetaInfConfiguration",
    "org.eclipse.jetty.webapp.FragmentConfiguration",
    "org.eclipse.jetty.plus.webapp.EnvConfiguration",
    "org.eclipse.jetty.webapp.JettyWebXmlConfiguration"
};
WebAppContext webapp = new WebAppContext();
webapp.setConfigurationClasses(configurationClasses);
webapp.setDescriptor("/path/to/webapp/WEB-INF/web.xml");
webapp.setContextPath("/");
webapp.setResourceBase("/path/to/webapp");
webapp.setClassLoader(Thread.currentThread().getContextClassLoader());

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

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