How to create an instance of ManagedThreadFactory in Glassfish V4.0

I call the rabbitMQ client (and not the server) from my Java code running in the Glassfish V4.0 container (build 89) to connect to the rabbitmq server on another machine. According to the client document rabbitmq, we must provide an instance of ManagedThreadFactory to connect rabbitmq to create threads.

I tried using DefaultManagedThreadFactory using

ctx.lookup ("Java: comp / DefaultManagedThreadFactory")

Failed to complete

Search failed for "java: comp / DefaultManagedThreadFactory" in SerialContext ...

Trying @Resource (lookup = "concurrent / __ DefaultManagedThreadFactory") results in an NPE.

I am not very versed in java EE, and I use this container as an interface for my web services. It is clear that I need to do something more / different. However, I could not find much more than

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

Can any Java EE expert tell me the right Voodoo spells to make this work?

Update-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

axel - in my case there is nothing special. What I'm looking at is just trying to use the rabbitmq java client inside GFV4. therefore to reproduce problems

  • You need to start the rabbitmq server
  • connect to the rabbitmq server using client code, which is part of the war deployed on GF
  • API rabbitmq (https://www.rabbitmq.com/api-guide.html) @see Factory. .
  • rabbitmq Github

ManagedThreadFactory GF Admin, .

-3

Factory - JNDI. , - . (, java EE7 Github)

/Hk2/CDI/ ..? , ?

+4
1

factory, Java EE, Glassfish 4:

@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
    }
}

asadmin list-containers

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

All Articles