在罐子之间注入

时间:2014-07-04 17:14:30

标签: java jar cdi weld

我有一个Jar,Helper项目,它有一个进程管理器,它控制线程etal的执行。它接收一个在触发时执行执行的接口。

我有另一个Jar,Service项目,它实现了Helper接口,并注入了一个工厂服务项目。

运行代码

时发生错误
processor = statementProcFactory.getProduct(modelMessage.getProcessType ()); 

通过Helper执行时。我尝试在Application Service中调用processMessage StatementToPRocessDequeueable,mocando消息,并且它没有问题。仅当SQSQueueRunnable调用processMessage时才会发生此错误。

帮助者的结构:

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

服务的结构

├── 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

主要服务:

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();
}

班级申请:

@Singleton
public class Application {

    @Inject
    private SQSQueueManager queueManager;

    @Inject
    private Provider<StatementToProcessDequeueable> statementProcDequeueProvider;


    public void run() {

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

addSQSDequeueable SQSQueueManager的方法:

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方法:

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);
}

实现接口ISQSDequeueable的StatementToProcessDequeueable方法processMessage

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");
    }
}

Class StatementProcessorFactory

@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();
}
}

Class StatementProcessorTypeLiteral

@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

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

实现IStatmentProcessor并具有限定符StatementProcessorType

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

}

错误:

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)

0 个答案:

没有答案