在ApacheServiceMix 7.0.0中,我使用Blueprint定义了以下路由:
<reference id="dataSource" interface="javax.sql.DataSource" filter="(dataSourceName=connectuserdata)" />
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource" />
</bean>
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<package>com.focuscura</package>
<dataFormats>
<!-- here we define a Json data format with the id jack and that it should use the TestPojo as the class type when
doing unmarshal. The unmarshalTypeName is optional, if not provided Camel will use a Map as the type -->
<json id="userdata" library="Jackson" />
</dataFormats>
<route id="connect.userdata_create">
<from uri="jetty:http://localhost:8881/userdata?httpMethodRestrict=POST"/>
<unmarshal ref="userdata"/>
<!--<process ref="scalaUserDataProcessor"/>-->
<log message="Received new userdata" />
<to uri="sql:INSERT INTO public."UserData" (lastname, firstname) VALUES (:#lastname , :#firstname)"/>
</route>
<route id="connect.userdata_get2">
<from uri="jetty:http://localhost:8881/userdata2?httpMethodRestrict=GET"/>
<to uri="sql:SELECT * FROM public."UserData" WHERE id = :#id"/>
<!--<process ref="scalaUserDataProcessor"/>-->
<marshal ref="userdata"/>
</route>
</camelContext>
数据源按照本文所述的说明安装为单独的服务:How can I install postgresqljdbc to work in Karaf OSGi?
这很好用!我可以将JSON发布到Jetty URL并将其插入数据库。 但是当我尝试从此端点获取数据时,我收到以下错误
org.osgi.service.blueprint.container.ServiceUnavailableException: The Blueprint container is being or has been destroyed: (&(dataSourceName=connectuserdata)(objectClass=javax.sql.DataSource))
at org.apache.aries.blueprint.container.ReferenceRecipe.getService(ReferenceRecipe.java:241)
at org.apache.aries.blueprint.container.ReferenceRecipe.access$000(ReferenceRecipe.java:56)
at org.apache.aries.blueprint.container.ReferenceRecipe$ServiceDispatcher.call(ReferenceRecipe.java:306)
at Proxyb6abdd30_6f59_4e89_a419_c4ff0558aa62.equals(Unknown Source)
at java.util.WeakHashMap.eq(WeakHashMap.java:287)
at java.util.WeakHashMap.get(WeakHashMap.java:401)
at org.springframework.jdbc.support.SQLErrorCodesFactory.getErrorCodes(SQLErrorCodesFactory.java:204)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.setDataSource(SQLErrorCodeSQLExceptionTranslator.java:140)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.
(SQLErrorCodeSQLExceptionTranslator.java:103) 在org.springframework.jdbc.support.JdbcAccessor.getExceptionTranslator(JdbcAccessor.java:99) 在org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:605) 在org.apache.camel.component.sql.SqlProducer.process(SqlProducer.java:100) at org.apache.camel.util.AsyncProcessorConverterHelper $ ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) 在org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145) 在org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460) 在org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) 在org.apache.camel.processor.Pipeline.process(Pipeline.java:121) 在org.apache.camel.processor.Pipeline.process(Pipeline.java:83) 在org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) 在org.apache.camel.component.jetty.CamelContinuationServlet.service(CamelContinuationServlet.java:191) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 在org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812) 在org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1669) 在org.eclipse.jetty.servlets.MultiPartFilter.doFilter(MultiPartFilter.java:146) 在org.apache.camel.component.jetty.CamelFilterWrapper.doFilter(CamelFilterWrapper.java:43) 在org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1652) 在org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) 在org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) 在org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) 在org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) 在org.eclipse.jetty.server.Server.handle(Server.java:499) 在org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311) 在org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) 在org.eclipse.jetty.io.AbstractConnection $ 2.run(AbstractConnection.java:544) 在org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) 在org.eclipse.jetty.util.thread.QueuedThreadPool $ 3.run(QueuedThreadPool.java:555) 在java.lang.Thread.run(Thread.java:745)
奇怪的是,当我用<to uri="sql:SELECT * FROM public."UserData" WHERE id = **:#id**"/>
替换<to uri="sql:SELECT * FROM public."UserData" WHERE id = **2**"/>
时
它再次正常工作,我得到了一个很好的用户nr 2返回的小JSON。
有关如何解决此问题的任何提示?
答案 0 :(得分:0)
问题不在我的路线或骆驼中。它是由PostgreSQL引起的。通过在我的路由中使用参数,我需要提供一个明确的类型转换。
为了实现这一点,我需要将:: bigint添加到我的路线中的参数:sql:SELECT lastname FROM public."UserData" WHERE id = :#id::bigint?dataSource=dataSource&allowNamedParameters=true
出于某种原因,Postgres的错误被Camel-sql吞噬了,我得到了关于数据源被破坏的奇怪消息。
感谢大家的有益评论和问题!