0

good day. i trying to use producer method to obtain proper bean for injection. it works fine when i call

Instance<HttpAmClientTransport> clienttrnsportI = instance.select(HttpAmClientTransport.class);
HttpAmClientTransport clienttrnsport = clienttrnsportI.get();

where HttpAmClientTransport.class is local interface of injected bean but if i want to call iterator on instance

Iterator<AmClientTransport> aaa = instance.iterator();

than this cause recursive factory class creation (with execution @Produces method)
there is discussion here with answer by Brian Blonski Java CDI: Dynamically selecting an implementation based on qualifier?

but in my case recursion appears when i get iterator , not obtain bean instance by .next()

also i need your advice. in fact i have several Transport Beans with their own local interfaces and several Clients beans in which i must inject particular transport. the type of client (rest soap or db) and transport qualifier (secure or not) will be given in config file and it may change. i thought that i could hardcoded some client configuration constraints in annotation with enumeration variable with necessary mapping , that would contains Transport interface class. those interfaces (one for each client bean ) would be implemented by Transport beans (and it may be that some client implements all interfaces as it suits for every client) but the is some question. how can i filter out all transport beans that implements that client-specific interface in producer method if i change

Instance<HttpAmClientTransport> clienttrnsport = instance.select(HttpAmClientTransport.class);

to

Instance<RestfulAmClientTransport> clienttrnsport = instance.select(RestfulAmClientTransport.class);

i have got isUnsatisfied() property true value (empty instance) but if i change those to

Instance<AmClientTransport> amtranses = instance.select(AmClientTransport.class);

than all check are false but get() method leads to recursion creation of whole producer class i know that i missing something about inheritance but what?

i would appreciate any advice you can provide me.

code part

restfullclient bean

public class  RESTfulAmClientBean 
extends BaseAmClient<RESTfulAmClientBean> 
implements RESTfulAmClient , Serializable{
@Inject
private Logger logger;

private static final long serialVersionUID = 1L;
private AmClientTransport trns;
//private AmClientRequestBuilder rb;
//private AmClientSerializer serializer;
private AmObjectApiSettingsManager amCliSettMgr;


@Inject
@AmClientTransportProducer
private AmClientTransport restfulAmClientTransport;

producer bean

@Local
public class AmClientTransportProducerImpl {
@Inject
private Logger logger;
@Inject
@Any
private Instance<AmClientTransport> trnsInst;
public AmClientTransportProducerImpl() {
}

@PostConstruct
private void initAmClientTransportProducerImpl(){
    logger.debug("создание экземпляря класса AmClientTransportProducerImpl");

}
@AmClientTransportProducer
@Produces
public AmClientTransport getAmClientTransport(@Any Instance<AmClientTransport> instance , InjectionPoint ip){
    logger.debug("запуск метода getAmClientTransport");
    logger.debug("isAmbig:" + instance.isAmbiguous() + " isUnSatt" + instance.isUnsatisfied());
    Instance<HttpAmClientTransport> clienttrnsport = instance.select(HttpAmClientTransport.class);
    boolean isAmbig = clienttrnsport.isAmbiguous();
    boolean isUnsat = clienttrnsport.isUnsatisfied();
    //Iterator<AmClientTransport> aaa = instance.iterator();
    // Here i have got recursion
    //AmClientTransport trns2 = aaa.next();
    HttpAmClientTransport trns = clienttrnsport.get();
    Instance<AmClientTransport> restfultranses = instance.select(AmClientTransport.class);
    isAmbig = restfultranses.isAmbiguous();
    isUnsat = restfultranses.isUnsatisfied();
    Iterator<AmClientTransport> it = restfultranses.iterator();
    // Here i have got recursion too
    if (
            (!restfultranses.isUnsatisfied())
            ){

    }
    return null;
}

AmClientTransport interface just interface

public interface AmClientTransport {}

client-specific transport interface that extends AmClientTransport

public interface RestfulAmClientTransport extends AmClientTransport {
public void execute();

}

Local particular transport Bean interface

@Local
public interface HttpAmClientTransport extends RestfulAmClientTransport{}

particular transport Bean implementation

@Stateless(mappedName = "HttpAmClientTransport")
@Local(HttpAmClientTransport.class)
@Default
public class HttpAmClientTransportImpl implements HttpAmClientTransport {

public HttpAmClientTransportImpl() {}

@Override
public void execute() {}
}
Community
  • 1
  • 1
  • I'll be honest, your question is hard to read. I suspect there's multiple questions in play here. RE the creation, its not specified that you will only get the instance created when calling `.next()` - that just happens to be how weld does it, OWB does it differently as you're seeing. – John Ament Oct 31 '16 at 17:22
  • @Ament so can you tell me how to (if it possible) to iterate through beans in instance variable in producer method , may be some OWB examples for produces factories or some documentation for OWB? in any case fist question is why calling the Itarator cause to create new fabric instance, and how to avoid it? – Scorpio The dark Oct 31 '16 at 19:05

0 Answers0