简体   繁体   English

如何在Glassfish V4.0中创建ManagedThreadFactory实例

[英]How to create ManagedThreadFactory instance inside Glassfish V4.0

I am calling rabbitMQ client (and not server) from inside my Java code running in Glassfish V4.0 (build 89) container to connect to rabbitmq server on another machine. 我从我在Glassfish V4.0(build 89)容器中运行的Java代码中调用rabbitMQ客户端(而不是服务器)来连接到另一台机器上的rabbitmq服务器。 As per rabbitmq client document, we should supply a ManagedThreadFactory instance to rabbitmq connection for creating threads. 根据rabbitmq客户端文档,我们应该为rabbitmq连接提供一个ManagedThreadFactory实例来创建线程。

I tried injecting DefaultManagedThreadFactory using 我尝试使用注入DefaultManagedThreadFactory

ctx.lookup("java:comp/DefaultManagedThreadFactory") ctx.lookup( “Java的:COMP / DefaultManagedThreadFactory”)

That failed with 失败了

Lookup failed for 'java:comp/DefaultManagedThreadFactory' in SerialContext... 查找在SerialContext中的'java:comp / DefaultManagedThreadFactory'失败了...

Trying @Resource(lookup="concurrent/__DefaultManagedThreadFactory") results in NPE. 尝试@Resource(lookup =“concurrent / __ DefaultManagedThreadFactory”)会导致NPE。

I am not heavy into java EE and I am using this container just as a frontend for my jersey web services. 我对Java EE并不重视,我正在使用这个容器作为我的球衣网络服务的前端。 Clearly I need to do something more/different. 显然,我需要做更多/不同的事情。 However I have been not able to locate much apart from 但是我一直无法找到太多的东西

https://blogs.oracle.com/arungupta/entry/create_managedexecutorservice_managedscheduledexecutorservice_managedthreadfactory_contextservice https://blogs.oracle.com/arungupta/entry/create_managedexecutorservice_managedscheduledexecutorservice_managedthreadfactory_contextservice

http://javahowto.blogspot.in/2011/02/how-to-create-and-look-up-thread-pool.html http://javahowto.blogspot.in/2011/02/how-to-create-and-look-up-thread-pool.html

Can any Java EE expert tell me the right Voodoo incantations to make this work? 任何Java EE专家都可以告诉我正确的Voodoo咒语使这项工作成功吗?

Update-1 更新1

@Resource(name = "concurrent/__defaultManagedThreadFactory")
ManagedThreadFactory threadFactory;

// set on rabbitmq connection Factory
factory.setThreadFactory(threadFactory);


java.lang.NullPointerException
at java.util.concurrent.ThreadPoolExecutor.<init>(ThreadPoolExecutor.java:1312)
at java.util.concurrent.ThreadPoolExecutor.<init>(ThreadPoolExecutor.java:1233)
at java.util.concurrent.Executors.newFixedThreadPool(Executors.java:114)
at com.rabbitmq.client.impl.ConsumerWorkService.<init>(ConsumerWorkService.java:36)

Update-2 更新2

axel - there is nothing special about my case. axel - 我的案子没有什么特别之处。 Just trying a rabbitmq java client inside GFV4 is what I am looking at. 只是在GFV4中尝试一个rabbitmq java客户端就是我所看到的。 so to reproduce the issues 所以重现问题

  • You need to run rabbitmq server 你需要运行rabbitmq服务器
  • connect to rabbitmq server using client code that is part of war deployed on GF 使用客户端代码连接到rabbitmq服务器,客户端代码是GF上部署的war的一部分
  • as per rabbitmq client API guide ( https://www.rabbitmq.com/api-guide.html ) @see Custom Thread Factory section. 根据rabbitmq客户端API指南( https://www.rabbitmq.com/api-guide.html)@see Custom Thread Factory部分。 They are delegating thread instantiation to the container. 他们将线程实例化委托给容器。
  • rabbitmq java client code is on Github rabbitmq java客户端代码在Github上

I can see ManagedThreadFactory name from GF Admin console so it should be there. 我可以从GF管理控制台看到ManagedThreadFactory名称,所以它应该在那里。

update-3 更新3

The Managed Thread Factory and Managed executor service can be located using JNDI. 可以使用JNDI定位托管线程工厂和托管执行程序服务。 However the same resources cannot be injected into my code that uses Jersey web services. 但是,相同的资源无法注入到使用Jersey Web服务的代码中。 The resource injection works with a simple servlet example (eg java EE7 example on Github) 资源注入使用一个简单的servlet示例(例如Github上的Java EE7示例)

is this due to some interplay of jersey/Hk2/CDI/weld etc.? 这是由于泽西/ Hk2 / CDI /焊接等的一些相互作用? I am looking for an authorative answer to why I cannot make resource injection work? 我正在寻找一个明确的答案,为什么我不能使资源注入工作?

I'm injecting the default managed thread factory like this in my Java EE projects that run on Glassfish 4: 我正在我在Glassfish 4上运行的Java EE项目中注入这样的默认托管线程工厂:

@Singleton
@Startup
public class Messenger {
    @EJB
    MyMessenger me;

    @Resource(name = "concurrent/__defaultManagedThreadFactory")
    ManagedThreadFactory threadFactory;

    @Resource
    ManagedExecutorService executorService;

    @PostConstruct
    public void postConstruct() {
        me.waitAndInitialize();
    }

    @Asynchronous
    public Future<?> waitAndInitialize() {
        try {
            final AtomicInteger done = new AtomicInteger(0);
            int i = 0;

            while (done.intValue() == 0 && i < 20) {
                i++;
                getExecutorService().submit(
                        new Runnable() {

                            @Override
                            public void run() {
                                int incrementAndGet = done.incrementAndGet();
                            }
                        });

                Thread.sleep(500);
            }

            if (done.intValue() == 0) {
                Logger.getAnonymousLogger().severe("Waited a long time for the ExecutorService do become ready, but it never did. Will not initialize!");
            } else {
                init();
            }
        } catch (Exception e) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Exception in waitAndInitialize: " + e.getMessage(), e);
        }

        return new AsyncResult<>(null);
    }

    protected void init() {
        connectionFactory.setExecutorService(executorService);
        connectionFactory.setThreadFactory(threadFactory);

        // connect to rabbit and listen to queues
    }
}

Output of asadmin list-containers 输出asadmin list-containers

List all known application containers
resources_ear
resources
ejb
weld
weld
grizzly
web
connector
webservices
appclient
jpa
jpa
ear
osgi
security

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

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