我有这样的代码:
@Autowired
private ProducerTemplate producerTemplate;
...
final List<Map<String, Object>> parsedLines = ...
parsedLines.forEach(parsedLine -> {
try {
producerTemplate.sendBody("mongodb:mongoBean?database=db_name&collection=collection_name&operation=save", parsedLine);
} catch (Exception e) {
logger.error("error:", e);
}
});
配置:
@Bean
public Mongo mongoBean() {
MongoCredential mongoCredential = MongoCredential.createMongoCRCredential("user_name", "db_name", "password".toCharArray());
return new MongoClient(new ServerAddress("127.0.0.1", 27017), Collections.singletonList(mongoCredential));
}
我也有依赖:
compile 'org.springframework.boot:spring-boot-starter-data-mongodb'
它产生跟踪:
org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: mongodb://mongoBean?collection=collection_name&database=db_name&operation=save due to: No component found with scheme: mongodb
at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:756) ~[camel-core-2.20.0.jar:2.20.0]
at org.apache.camel.impl.DefaultProducerTemplate.resolveMandatoryEndpoint(DefaultProducerTemplate.java:497) ~[camel-core-2.20.0.jar:2.20.0]
at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:167) ~[camel-core-2.20.0.jar:2.20.0]
at com.debeers.mis.upload.route.DummyRoute$1.lambda$process$0(DummyRoute.java:100) ~[classes/:na]
at java.util.ArrayList.forEach(ArrayList.java:1249) ~[na:1.8.0_111]
at com.debeers.mis.upload.route.DummyRoute$1.process(DummyRoute.java:98) ~[classes/:na]
at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63) ~[camel-core-2.20.0.jar:2.20.0]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) ~[camel-core-2.20.0.jar:2.20.0]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) ~[camel-core-2.20.0.jar:2.20.0]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) ~[camel-core-2.20.0.jar:2.20.0]
at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:452) ~[camel-core-2.20.0.jar:2.20.0]
at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:219) ~[camel-core-2.20.0.jar:2.20.0]
at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:183) ~[camel-core-2.20.0.jar:2.20.0]
at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:174) ~[camel-core-2.20.0.jar:2.20.0]
at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:101) ~[camel-core-2.20.0.jar:2.20.0]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_111]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) ~[na:1.8.0_111]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) ~[na:1.8.0_111]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) ~[na:1.8.0_111]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_111]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_111]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_111]
我做错了什么以及如何解决?
附言
应用程序也打印到日志:
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.3.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.3.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.3.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_111]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_111]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_111]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_111]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_111]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_111]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_111]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_111]
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.3.jar:na]
at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.3.jar:na]
... 3 common frames omitted