Tenho um projeto Jar, Helper, que possui um gerenciador de processos, onde controla a execução de threads etal. Onde ele recebe uma interface que realiza a execução quando acionado.
Tenho outro projeto Jar, Service, que implementa a interface da Helper, e injeta um factory do próprio projeto Service.
O erro ocorre quando é executado o código
processor = statementProcFactory.getProduct(modelMessage.getProcessType());
quando executado via Helper. Testei chamar o processMessage do StatementToPRocessDequeueable dentro do Application do Service, mocando o message, e funcionou sem problemas. O erro ocorre somente quando o processMessage é chamado pelo SQSQueueRunnable.
A estrutura do Helper:
├── src
│ ├── main
│ │ ├── java
│ │ │ └── br
│ │ │ └── com
│ │ │ └── aws
│ │ │ └── sqs
│ │ │ ├── ISQSDequeueable.java
│ │ │ ├── SQSDequeueableException.java
│ │ │ ├── SQSQueueManager.java
│ │ │ └── SQSQueueRunnable.java
│ │ └── resources
│ │ └── META-INF
│ │ └── beans.xml
E a estrutura do Service
├── 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 do 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());
}
}
Método addSQSDequeueable do 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();
}
Método run do SQSRunnable:
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);
}
Método processMessage do StatementToProcessDequeueable que implementa a interface ISQSDequeueable
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");
}
}
Classe 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();
}
}
Classe 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;
}
}
Interface StatementProcessorType
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface StatementProcessorType {
public String value();
}
Classe que implementa o IStatmentProcessor e tem o qualifier do StatementProcessorType
@StatementProcessorType("Banri")
@Stateless
public class BanriProcessor implements IStatementProcessor {
public List<OperationCard> process(InputStream statementFile, Retail retail) {
// TODO Auto-generated method stub
return null;
}
}
Erro:
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)