Erro Task VRaptor

7 respostas
D

Segue o código:

Task:

@PrototypeScoped
@Scheduled(cron = "* 0/1 * * * ?")
public class HelloTask implements Task {
    private ManagerServiceUser managerUser;

    HelloTask(ManagerServiceUser managerUser) {
        this.managerUser = managerUser;
    }

ManagerServiceUser:

@Component
public class ManagerServiceUser {
}

Erro:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloTask': Unsatisfied dependency expressed through constructor argument with index 0 of type [gov.br.ufla.urubu.managers.ManagerServiceUser]: : Error creating bean with name 'managerServiceUser': Unsatisfied dependency expressed through constructor argument with index 0 of type [gov.br.ufla.urubu.services.DAO.ServiceUserDAO]: : Error creating bean with name 'serviceUserDAOImpl': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceUserDAOImpl': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'managerServiceUser': Unsatisfied dependency expressed through constructor argument with index 0 of type [gov.br.ufla.urubu.services.DAO.ServiceUserDAO]: : Error creating bean with name 'serviceUserDAOImpl': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceUserDAOImpl': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

Já tentei mudar várias anotações, mas sem sucesso.

Obrigado.

7 Respostas

Lucas_Cavalcanti

vc está usando um plugin do vraptor pra tasks?

quem está criando esse HelloTask?

em todo caso essas tasks não rodam numa request, e o seu componente ManagerServiceUser é de request.

pra resolver isso, vc pode fazer:

  • ao invés da Task executar o código, ela só dispara uma requisição para um controller que vai executar o código.

  • mudar o escopo do ManagerServiceUser para @PrototypeScoped ou @ApplicationScoped. Se ele depende do banco de dados (EntityManager ou Session), isso não é viável.

wpivotto

É isso mesmo, suas dependências não podem ser @RequestScoped.

Se você precisar rodar uma task nesse escopo faça o seguinte:

@Resource
class HelloController {

@Post
@Scheduled(cron = "* 0/1 * * * ?", id = "HelloTask") 
public void hello(List<String> params) {
   ....
}

}

//vc pode passar parâmetros e receber no seu método
scheduler.include("params[0]", "value1", "HelloTask").include("params[1]", "value2", "HelloTask");

Use a versão 1.0.3 que você pode baixar aqui https://oss.sonatype.org/content/groups/public/br/com/prixma/vraptor-tasks/1.0.3/

D

Olá, obrigado pelas respostas.

Sim, estou usando o plugin Task do vraptor.

A HelloTask é chamada automaticamente, ninguém ‘cria’ ela.

Bom, o ManagerServiceUser chama métodos de outra classe que precisam do banco de dados.

A minha Task vai enviar dados do meu banco para outro server que vai guarda-los.

Então ficaria assim?

@Resource  
class GeoController {  
  
ManagerServiceUser managerUser;

@Post  
@Scheduled(cron = "* 0/1 * * * ?", id = "GeoTask")   
public void sendToGeo() {  
   managerUser.getPhotos(); //Pega do banco

   //HTTPCLIENT stuff.
}  
  
}

Obrigado pelas respostas.
Hoje à tarde vou postar um feedback.

D

Olá, ainda não consegui fazer funcionar.

Olhando a documentação (https://github.com/wpivotto/vraptor-tasks/) fiquei me perguntando se devo usar Transactional Task (JPA) ou Transactional Task (Hibernate) ou se pode ser Simple Task mesmo.

Bom, entendi que não posso chamar o ManagerServiceUser dentro da minha Task, a sugestão foi enviar uma requisição para um Controller, como eu faria isso?
Algum exemplo?

Obrigado.

D

Opa, consegui !
Bastava chamar a URL, usei HttpClient para tal.

Att.

D

Bom, apesar de ter conseguido dessa maneira, ficou uma implementação feia, tem alguma outra maneira de acessar o método dentro do Controller sem enviar uma requisição HTTP pra sua URL ?

wpivotto

Você não precisa enviar uma requisição para a URL. O que acontece é que o plugin somente registra as tasks após a primeira requisição ser recebida (um interceptor faz o schedule).
Isso porque é necessário uma request para montar o path (host, protocolo, porta…).

O que poderia ser implementado é ler o host de um arquivo de configuração. Abre uma issue no github por favor.

Criado 12 de março de 2013
Ultima resposta 14 de mar. de 2013
Respostas 7
Participantes 3