Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9249983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:20:09+00:00 2026-06-18T10:20:09+00:00

Is it possible to obtain a CDI conversation instance programatically only knowing that the

  • 0

Is it possible to obtain a CDI conversation instance programatically only knowing that the current thread is the one being used to process the CDI request associated with the wanted conversation? And if possible then how?

In particular, what I want to do is this:

@ConversationScoped
public class UnitOfWork {...}

public class Client {
    @Inject transient UnitOfWork uof;
...
}

public class Room {
    @Inject transient UnitOfWork uof;
...
}

but using a programatic mechanism to initialize the uof instance variables instead of applying the @Inject annotation (because Client and Room are entities and they doesn’t support injection).

I already tryed to inject the UnitOfWork by means of a BeanManager obtained by the following static method:

public static <B> B getManagedBean(Class<B> type, Annotation... qualifiers) {
    try {
        BeanManager beanManager = InitialContext.doLookup("java:comp/BeanManager");
        Set<Bean<?>> beans = beanManager.getBeans(type, qualifiers);
        Bean<B> bean = (Bean<B>) beanManager.resolve(beans);
        CreationalContext<B> cc = beanManager.createCreationalContext(bean);
        return bean.create(cc);
    } catch (NamingException e) {
        throw new RuntimeException("", e);
    }
}

but the problem is that beans given by means of the above method are new ones (every call gives a new instance), and I need that Client and Room share the same conversation scoped instance of UnitOfWork.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:20:10+00:00Added an answer on June 18, 2026 at 10:20 am

    The answer was very near but I overlooked it.
    Yes, it is possible to obtain the bean class instance of any bean contained by any of the active contexts (the ones associated with the current thread), by means of the BeanManager.
    This method does the job:

    public static <B> B getContextualBeanInstance(Class<B> type, Annotation... qualifiers) {
        try {
            BeanManager beanManager = InitialContext.doLookup("java:comp/BeanManager");
            Set<Bean<?>> beans = beanManager.getBeans(type, qualifiers);
            Bean<?> bean = beanManager.resolve(beans);
            CreationalContext<?> cc = beanManager.createCreationalContext(bean);
            return (B) beanManager.getReference(bean, type, cc);
        } catch (NamingException e) {
            throw new RuntimeException("", e);
        }
    }
    

    The only difference with the method I mentioned in the question post is that this one uses BeanManager#getReference(..) instead of Bean#create(..).

    If you want to support parameterized bean types, change the type of the type parameter from Class<B> to Type.

    If the bean is @Dependent scoped, you should take care of the destruction of the bean class instance to avoid memory leaks. Here I explain how to do it nicely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using reflection it is possible to obtain the call stack (apart from that
Is it possible to obtain the name of the current procedure/function as a string,
Is it possible to obtain a reference/pointer to a class type and enforce that
I need to know if it is possible to obtain the current execution node?
Is it possible to obtain an instance of Class<SomeGenericClass<SomeType>> ? For example an instance
is it possible to obtain the instance name of a class from the class
The question: Is it possible to obtain the size that an autorotated UIView will
Is it possible to obtain the website's IP address that you are connecting to
I was wondering whether it was possible to obtain the current user object and
Is it possible to obtain values that match a range of keys in Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.