Spring Boot不会通过spring集成amqp inbound-channel-adapter释放tomcat.jdbc.pool连接

时间:2015-03-09 00:16:48

标签: tomcat spring-boot spring-integration spring-jdbc spring-amqp

我只是认为我有一些基本的东西,但是对于我的生活,我无法弄明白。我试图使用Spring Integration从amqp接收:inbound-channel-adapter,它最终使用“jdbcTemplate.getDataSource()。getConnection()。prepareStatement(sql).execute”调用一个调用Oracle存储过程的服务。 );“

让所有这些都通过包含spring-boot-starter-web的Spring Boot应用程序运行,因此它会引入嵌入式tomcat,我的Spring Boot应用程序还包括spring-boot-starter-amqp,spring-boot-starter-集成,spring-boot-starter-jdbc让Spring Boot为我做了所有的魔术...... SB ROCKS!

我还必须在我的pom.xml文件中特别包含spring-integration-amqp,以便出于某种原因使用XML:

    <!-- Required in order to expose amqp xml schema namespace -->
    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-amqp</artifactId>
    </dependency>

一切似乎都很好;但是,通道似乎没有放弃JDBC连接。我最终得到一个org.apache.tomcat.jdbc.pool.PoolExhaustedException。一旦通道从队列中提供了“spring.datasource.max-active”消息,就会发生这种情况。我通过玩这个属性证实了这一点,它总是发生在我设置的任何数字上。在完成db调用之后,我记录了service-activator中调用的服务方法,所以我知道它们已完成,但数据库连接显然永远不会被传回池...任何想法?以下是我项目的相关摘要:

application.properties:

spring.datasource.url = jdbc:oracle:thin:@[myserver].com:1521:[SID]
spring.datasource.username = scrubbed
spring.datasource.password = scrubbed
spring.datasource.driver-class-name = oracle.jdbc.driver.OracleDriver
spring.datasource.max-active=50 
...
spring.rabbitmq.port = 5672
spring.rabbitmq.addresses = clusered.server1, clusered.server1
spring.rabbitmq.username = scrubbed
spring.rabbitmq.password = scrubbed

服务:

@Autowired
private JdbcTemplate jdbcTemplate;

@Override
public void executeDbJob(String dbProcedureCall) {
    log.info("Executing Job id: {} Database Call: {}", jobId, dbProcedureCall);
    executeStoredProcedure(dbProcedureCall);
    log.info("Finished DB call for job id: {} Database Call: {}", jobId, dbProcedureCall);
}
/**
 * This wraps a stored procedure call just as it is, parameter values included into an anonymous
 * pl/sql block. This could be a pretty big security issue so we will want to scrub the
 * procedure coming in to ensure it doesn't have additional embedded SQL.
 */
private void executeStoredProcedure(final String procedure) {
    final String sql = "begin ".concat(procedure).concat(" end;");
    try {
        jdbcTemplate.getDataSource().getConnection().prepareStatement(sql).execute();
    } catch (SQLException e) {
        log.error("Could not execute procedure call: {} raised: {}", sql, e);
    }
}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:stream="http://www.springframework.org/schema/integration/stream"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:amqp="http://www.springframework.org/schema/integration/amqp" 
    xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
                http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
                http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd
                http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
                http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
                http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd"
    default-lazy-init="false">

    <beans:description>Recommendation Engine provider account event flow</beans:description>

    <!-- ********** Consumer flows ********** -->

    <!-- Note: The rabbitConnectionFactory is setup automagically just like rabbitTemplate by spring-boot-starter-amqp -->
    <amqp:inbound-channel-adapter id="initiateJobRequest" channel="initiateJobRequestChannel"
        message-converter="jsonMessageConverter" queue-names="bc.initiate" connection-factory="rabbitConnectionFactory"
        auto-startup="true" concurrent-consumers="1" acknowledge-mode="AUTO" error-handler="loggingErrorHandler" task-executor="amqpClientExecutor"/>

    <!-- Add queue capacity to make channel pollable -->
    <channel id="initiateJobRequestChannel">
        <queue capacity="32" />
        <interceptors>
            <wire-tap channel="amqpDebugLogger" />
        </interceptors>
    </channel>

     <service-activator id="initiateJobRequestExtractor" input-channel="initiateJobRequestChannel"
        output-channel="nullChannel" ref="jobExecutorService" method="executeDbJob">
        <poller ref="initiateJobPoller" />
    </service-activator>

    <!-- ********** Loggers ********** -->

     <logging-channel-adapter id="amqpDebugLogger" level="DEBUG" auto-startup="true"
        log-full-message="true" />


    <!-- ********** Executors ********** -->

     <task:executor id="amqpClientExecutor" pool-size="8" queue-capacity="0" rejection-policy="CALLER_RUNS" />

    <poller id="initiateJobPoller" task-executor="initiateJobTaskExecutor" fixed-rate="50" receive-timeout="1000" />
    <task:executor id="initiateJobTaskExecutor" pool-size="16" queue-capacity="0" rejection-policy="CALLER_RUNS" />

</beans:beans>

在有用的情况下添加例外等等:

org.apache.tomcat.jdbc.pool.PoolExhaustedException: [initiateJobTaskExecutor-5] Timeout: Pool empty. Unable to fetch a connection in 30 seconds, none available[size:50; busy:50; idle:0; lastwait:30000].
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:672)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.getConnection(ConnectionPool.java:186)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:127)
    at com.everbridge.bce.jobexecutor.service.impl.JobExecutorServiceImpl.executeStoredProcedure(JobExecutorServiceImpl.java:90)
    at com.everbridge.bce.jobexecutor.service.impl.JobExecutorServiceImpl.executeDbJob(JobExecutorServiceImpl.java:55)
    at sun.reflect.GeneratedMethodAccessor58.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:112)
    at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:102)
    at org.springframework.expression.spel.ast.MethodReference.access$000(MethodReference.java:49)
    at org.springframework.expression.spel.ast.MethodReference$MethodValueRef.getValue(MethodReference.java:342)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:131)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:330)
    at org.springframework.integration.util.AbstractExpressionEvaluator.evaluateExpression(AbstractExpressionEvaluator.java:164)
    at org.springframework.integration.util.MessagingMethodInvokerHelper.processInternal(MessagingMethodInvokerHelper.java:276)
    at org.springframework.integration.util.MessagingMethodInvokerHelper.process(MessagingMethodInvokerHelper.java:142)
    at org.springframework.integration.handler.MethodInvokingMessageProcessor.processMessage(MethodInvokingMessageProcessor.java:75)
    at org.springframework.integration.handler.ServiceActivatingHandler.handleRequestMessage(ServiceActivatingHandler.java:71)
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:99)
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
    at org.springframework.integration.endpoint.PollingConsumer.handleMessage(PollingConsumer.java:74)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:219)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:55)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:149)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:146)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:298)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:52)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:1)

我想出了这个......感谢Pavel Horal对Mark关于&#34; C3PO连接池的问题的回应 - 没有发布连接&#34; (以及马克要求),这让我知道我的问题是什么以及如何解决它。事实证明这是因为我要求JdbcTemplate(jdbcTemplate.getDataSource().getConnection().prepareStatement(sql).execute())建立数据库连接;我用jdbcTemplate.update(sql);替换它,工作