Injection between jars

2

I have a Jar, Helper project, which has a process manager, where it controls the execution of etal threads. Where it gets an interface that performs execution when triggered.

I have another Jar, Service project that implements the Helper interface, and injects a factory from the Service project itself.

The error occurs when the code is executed

processor = statementProcFactory.getProduct(modelMessage.getProcessType());

when run via Helper. I tried calling the ProcessMessage from the StatementToPRocessDequeueable within the Service Application, moving the message, and it ran smoothly. The error occurs only when the processMessage is called by SQSQueueRunnable.

The helper structure:

├── src
│   ├── main
│   │   ├── java
│   │   │   └── br
│   │   │       └── com
│   │   │           └── aws
│   │   │               └── sqs
│   │   │                   ├── ISQSDequeueable.java
│   │   │                   ├── SQSDequeueableException.java
│   │   │                   ├── SQSQueueManager.java
│   │   │                   └── SQSQueueRunnable.java
│   │   └── resources
│   │       └── META-INF
│   │           └── beans.xml

And the Service framework

├── src
│   ├── main
│   │   ├── java
│   │   │   └── br
│   │   │       └── com
│   │   │           └── reconciliation
│   │   │               ├── service
│   │   │               │   ├── Application.java
│   │   │               │   ├── MainService.java
│   │   │               │   └── statement
│   │   │               │       └── processor
│   │   │               │           ├── IStatementProcessor.java
│   │   │               │           ├── processors
│   │   │               │           │   ├── BanriProcessor.java
│   │   │               │           │   ├── CieloProcessor.java
│   │   │               │           │   ├── RedeCreditProcessor.java
│   │   │               │           │   └── RedeDebitProcessor.java
│   │   │               │           ├── StatementProcessorFactory.java
│   │   │               │           ├── StatementProcessorType.java
│   │   │               │           └── StatementProcessorTypeLiteral.java
│   │   │               └── sqsdequeueable
│   │   │                   ├── StatementToProcessDequeueable.java
│   │   │                   └── StatementToProcessModel.java
│   │   └── resources
│   │       ├── aws.properties
│   │       └── META-INF
│   │           └── beans.xml

Main Service:

public static void main(String[] args) {
    timer = new Timer();
    timer.schedule(new EchoTask(), 0, 1000);

    Weld weld = new Weld();
    WeldContainer container = weld.initialize();
    Application application = container.instance().select(Application.class).get();
    application.run();
    weld.shutdown();
}

Casse Application:

@Singleton
public class Application {

    @Inject
    private SQSQueueManager queueManager;

    @Inject
    private Provider<StatementToProcessDequeueable> statementProcDequeueProvider;


    public void run() {

        queueManager.addSQSDequeueable(statementProcDequeueProvider.get());
    }
}

SQSQueueManager addSQSDequeueable method:

public void addSQSDequeueable(ISQSDequeueable dequeueable) {

    SQSQueueRunnable runnable = runnableProvider.get();

    runnable.setDequeueable(dequeueable);

    Thread th = new Thread(runnable);

    dictionaryDequeueables.put(dequeueable, runnable);

    dequeueable.startRunning();

    th.start();
}

SQSRunnable run method:

public void run() {
    ReceiveMessageRequest request = new ReceiveMessageRequest(
            dequeueable.getQueue());

    do {

        List<Message> messages = sqsAmazon.receiveMessage(request)
                .getMessages();

        for (Message message : messages) {
            processed = true;

            Thread th = new Thread(new Runnable() {

                private Message message;

                public void run() {
                    try {
                        dequeueable.processMessage(message);
                    } catch (SQSDequeueableException e) {
                        // TODO Mover mensagem para fila de erros
                        e.printStackTrace();
                    }

                    sqsAmazon.deleteMessage(new DeleteMessageRequest(
                            dequeueable.getQueue(), message
                                    .getReceiptHandle()));
                }

                public Runnable setMessage(Message message) {
                    this.message = message;
                    return this;
                }

            }.setMessage(message));

            th.run();
        }

        try {
            Thread.sleep(this.getSleepThread());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (isRunning == false)
            dequeueable.stopRunning();

    } while (isRunning);
}

StatementToProcessDequeueable processMessage method that implements the ISQSDequeueable interface

public void processMessage(Message message) throws SQSDequeueableException {

    logger.debug("Processando mensagem");

    StatementToProcessModel modelMessage = this.generateModel(message.getBody());

    if (modelMessage != null) {
        processor = statementProcFactory.getProduct(modelMessage.getProcessType());

        logger.debug("Processor gerado: " + processor);

        if (processor != null) {

            Statement statement = new Statement();

            Retail retail = repoRetail.getRetailByID(modelMessage.getRetailId());

            List<OperationCard> operations = processor.process(
                    statement.getInputStream(), retail);

            for (OperationCard operation : operations) {

            }

        } else {
            throw new SQSDequeueableException(String.format(
                    "O tipo de processador %s está inválido", processor));
        }
    } else {
        logger.debug("Atributos inválidos da mensagem");

        throw new SQSDequeueableException(
                "Mensagem não possuí todos atributos necessários");
    }
}

StatementProcessorFactory class

@Singleton
public class StatementProcessorFactory {

@Inject
@Any
private Instance<IStatementProcessor> products;

public IStatementProcessor getProduct(String type) {
    StatementProcessorTypeLiteral literal = new StatementProcessorTypeLiteral(type);
    Instance<IStatementProcessor> typeProducts = products.select(literal);
    return typeProducts.get();
}
}

StatementProcessorTypeLiteral class

@SuppressWarnings("serial")
public class StatementProcessorTypeLiteral extends
    AnnotationLiteral<StatementProcessorType> implements StatementProcessorType {

private String type;

public StatementProcessorTypeLiteral(String type) {
    this.type = type;
}

public String value() {
    return type;
}
}

StatementProcessorType interface

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface StatementProcessorType {
    public String value();
}

Class that implements the IStatmentProcessor and has the StatementProcessorType qualifier

@StatementProcessorType("Banri")
@Stateless
public class BanriProcessor implements IStatementProcessor {
    public List<OperationCard> process(InputStream statementFile, Retail retail) {
    // TODO Auto-generated method stub
    return null;
}

}

Error:

Exception in thread "Thread-2" org.jboss.weld.exceptions.UnsatisfiedResolutionException: WELD-001308: Unable to resolve any beans for Types: [interface br.com.agilebox.gestorfiscal.reconciliation.service.statement.processor.IStatementProcessor]; Bindings: [QualifierInstance{annotationClass=interface br.com.agilebox.gestorfiscal.reconciliation.service.statement.processor.StatementProcessorType, values={[BackedAnnotatedMethod] public abstract br.com.agilebox.gestorfiscal.reconciliation.service.statement.processor.StatementProcessorType.value()=Banri}, hashCode=-1507802905}, QualifierInstance{annotationClass=interface javax.enterprise.inject.Any, values={}, hashCode=-2093968819}]
at org.jboss.weld.manager.BeanManagerImpl.getBean(BeanManagerImpl.java:854)
at org.jboss.weld.bean.builtin.InstanceImpl.get(InstanceImpl.java:75)
at br.com.agilebox.gestorfiscal.reconciliation.service.statement.processor.StatementProcessorFactory.getProduct(StatementProcessorFactory.java:19)
at br.com.agilebox.gestorfiscal.reconciliation.sqsdequeueable.StatementToProcessDequeueable.processMessage(StatementToProcessDequeueable.java:31)
at br.com.agilebox.gestorfisccal.aws.sqs.SQSQueueRunnable.run(SQSQueueRunnable.java:64)
at java.lang.Thread.run(Thread.java:745)
    
asked by anonymous 28.06.2014 / 22:28

1 answer

1

Each Jar that has beans CDI to be injected must have a beans.xml file in the META-INF folder, as documentation .

It can be a very empty file. Try to put it on both Jars.

    
30.06.2014 / 23:29