简体   繁体   中英

Creating message queue in spring boot using apache camel

I'm very newbie to this messaging queue and just started learning some basic stuffs in this.

So for our spring boot application we followed an architecture like contoller talks to service & service talks to repository so here i have to create one controller that will accept a class DTO as a json and post these information to the message queue specified in the apache camel. I'm following this link ! for my reference that works well but when i tried to implement it in my project , it saying me an error listed below.

Error

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'trackerQueueController': Unsatisfied dependency expressed through field 'camelContext'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.apache.camel.CamelContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I have created an controller,routes & processor as below:

Controller

@RestController
@RequestMapping("/deviceinfo")
public class TrackerQueueController {
@Autowired
CamelContext camelContext;

@Autowired
private
ProducerTemplate producerTemplate;

@PostMapping()
public void startCamel(@RequestBody FieldUpdate fieldUpdate) {
    producerTemplate.sendBody("activemq:topic:in", fieldUpdate);
}
}

Routes

   @Component
public class TrackerQueueRoutes extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("activemq:topic:in")
                .process(new TrackerProcessor() {
                    @Override
                    public void process(Exchange exchange) throws 
Exception {
                        log.info("I'm in");
                        FieldUpdate body = 
exchange.getIn().getBody(FieldUpdate.class);
                    log.info("Hello from camel processed message! 
Received payload: {}" , body.getSerialNumber());

exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 
HttpStatus.ACCEPTED);
                    }
            });
    }
}

Processor

public class TrackerProcessor implements Processor {
    @Override
   public void process(Exchange exchange) throws Exception {

   }
}

Can any one provide me some tutorial link that fulfil my need or any ideas.

As Claus Ibsen suggested in the comments, you have to add these dependencies to your POM file

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring-boot-starter</artifactId>
    <version>[camel-version]</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • camel-spring-boot-starter automatically starts a CamelContext for you, discovers routes etc
  • spring-boot-starter-web keeps your application running by listening for web requests. Otherwise it would immediately shut down after startup because there is nothing to execute.

Since your Camel route class is correctly annotated ( @Component ) and subclassed ( extends RouteBuilder ), it should be auto-discovered by the Camel SpringBoot starter.

See the Camel-SpringBoot docs for all these topics and more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM