3
respostas

Erro ao rodar a classe RotaHttpPollingNegociacoes

Prezados estou obtendo erro ao rodar o código abaixo

package br.com.caelum.camel;

import java.text.SimpleDateFormat;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.xstream.XStreamDataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.thoughtworks.xstream.XStream;

public class RotaHttpPollingNegociacoes {
    public static void main(String[] args) throws Exception {
        final XStream xstream = new XStream();
        xstream.alias("negociacao", Negociacao.class);

        SimpleRegistry registro = new SimpleRegistry();
        registro.put("mysql", criaDataSource());

        CamelContext context = new DefaultCamelContext(registro);
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("timer://negociacoes?fixedRate=true&delay=3s&period=360s").
                to("http4://argentumws.caelum.com.br/negociacoes").
                  convertBodyTo(String.class).
                  unmarshal(new XStreamDataFormat(xstream)).
                  split(body()).
                  process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        Negociacao negociacao = exchange.getIn().getBody(Negociacao.class);
                        exchange.setProperty("preco", negociacao.getPreco());
                        exchange.setProperty("quantidade", negociacao.getQuantidade());
                        String data = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(negociacao.getData().getTime());
                        exchange.setProperty("data", data);
                    }
                  }).
                  setBody(simple("insert into negociacao(preco, quantidade, data) values (${property.preco}, ${property.quantidade}, '${property.data}')")).
                  log("${body}").
                  delay(1000).
            to("jdbc:mysql");
            }
        });

        context.start();
        Thread.sleep(2000);        
    }

    private static MysqlConnectionPoolDataSource criaDataSource() {
        MysqlConnectionPoolDataSource mysqlDs = new MysqlConnectionPoolDataSource();
        mysqlDs.setDatabaseName("camel");
        mysqlDs.setServerName("localhost");
        mysqlDs.setPort(3306);
        mysqlDs.setUser("root");
        mysqlDs.setPassword("admin");
        return mysqlDs;
    }

}

Segue o erro:

Exception in thread "main" org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> Split[simple{${body}} -> [process[Processor@0x5669c5fb], SetBody[simple{Simple: insert into negociacao(preco, quantidade, data) values (${property.preco}, ${property.quantidade}, '${property.data}')}], Log[${body}], Delay[{1000} -> [To[jdbc:mysql]]]]] <<< in route: Route(route1)[[From[timer://negociacoes?fixedRate=true&delay... because of Failed to resolve endpoint: jdbc://mysql due to: No component found with scheme: jdbc
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1072)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:196)
    at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:944)
    at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:3245)
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:2968)
    at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:172)
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2799)
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2795)
    at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:2818)
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:2795)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:2764)
    at br.com.caelum.camel.RotaHttpPollingNegociacoes.main(RotaHttpPollingNegociacoes.java:50)
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: jdbc://mysql due to: No component found with scheme: jdbc
    at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:593)
    at org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:79)
    at org.apache.camel.model.RouteDefinition.resolveEndpoint(RouteDefinition.java:211)
    at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:107)
    at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:113)
    at org.apache.camel.model.SendDefinition.resolveEndpoint(SendDefinition.java:62)
    at org.apache.camel.model.SendDefinition.createProcessor(SendDefinition.java:56)
    at org.apache.camel.model.ProcessorDefinition.createProcessor(ProcessorDefinition.java:483)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessorImpl(ProcessorDefinition.java:446)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:413)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:165)
    at org.apache.camel.model.ProcessorDefinition.createChildProcessor(ProcessorDefinition.java:184)
    at org.apache.camel.model.DelayDefinition.createProcessor(DelayDefinition.java:75)
    at org.apache.camel.model.ProcessorDefinition.createProcessor(ProcessorDefinition.java:483)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessorImpl(ProcessorDefinition.java:446)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:413)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:165)
    at org.apache.camel.model.ProcessorDefinition.createChildProcessor(ProcessorDefinition.java:184)
    at org.apache.camel.model.SplitDefinition.createProcessor(SplitDefinition.java:99)
    at org.apache.camel.model.ProcessorDefinition.makeProcessorImpl(ProcessorDefinition.java:534)
    at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:495)
    at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:219)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1069)
    ... 12 more
3 respostas

Opa, vc adicionou a dependência => http://mvnrepository.com/artifact/org.apache.camel/camel-jdbc ?

Olá Alberto adicionei sim essa dependecia, e na console nao volta nada. Segue novamente minha classe completa:

package br.com.caelum.camel;

import java.text.SimpleDateFormat;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.xstream.XStreamDataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.thoughtworks.xstream.XStream;

public class RotaHttpPollingNegociacoes {
    public static void main(String[] args) throws Exception {
        final XStream xstream = new XStream();
        xstream.alias("negociacao", Negociacao.class);

        SimpleRegistry registro = new SimpleRegistry();
        registro.put("mysql", criaDataSource());

        CamelContext context = new DefaultCamelContext(registro);
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("timer://negociacoes?fixedRate=true&delay=3s&period=360s").
                to("http4://argentumws.caelum.com.br/negociacoes").
                  convertBodyTo(String.class).
                  unmarshal(new XStreamDataFormat(xstream)).
                  split(body()).
                  log("${body}").
                  process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        Negociacao negociacao = exchange.getIn().getBody(Negociacao.class);
                        exchange.setProperty("preco", negociacao.getPreco());
                        exchange.setProperty("quantidade", negociacao.getQuantidade());
                        String data = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(negociacao.getData().getTime());
                        exchange.setProperty("data", data);
                    }
                  }).
                  setBody(simple("insert into negociacao(preco, quantidade, data) values (${property.preco}, ${property.quantidade}, '${property.data}')")).
                  log("${body}").
                  delay(1000).
            to("jdbc:mysql");
            }
        });

        context.start();
        //Thread.sleep(2000);        
    }

    private static MysqlConnectionPoolDataSource criaDataSource() {
        MysqlConnectionPoolDataSource mysqlDs = new MysqlConnectionPoolDataSource();
        mysqlDs.setDatabaseName("camel");
        mysqlDs.setServerName("localhost");
        mysqlDs.setPort(3306);
        mysqlDs.setUser("root");
        mysqlDs.setPassword("root");
        return mysqlDs;
    }

}

Também me deparei com esse erro, mas o problema foi resolvido quando utilizei a versão correta do conector mysql. Verifique a versão do mysql instalado na sua maquina.

A versão que funcionou na minha máquina foi essa:

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.43</version>
</dependency>