gpt4 book ai didi

tomcat - Spring Boot 不通过 spring 集成 amqp inbound-channel-adaptor 放开 tomcat.jdbc.pool 连接

转载 作者:行者123 更新时间:2023-11-28 21:59:51 25 4
gpt4 key购买 nike

我敢打赌我在这方面遗漏了一些基本的东西,但我一直没能弄明白。我正在尝试使用 Spring Integration 从 amqp:inbound-channel-adaptor 接收数据,它最终调用了一个服务,该服务使用“jdbcTemplate.getDataSource().getConnection().prepareStatement(sql).execute( );"

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

出于某种原因,为了使用 XML,我确实还必须在我的 pom.xml 文件中专门包含 spring-integration-amqp:

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

一切正常;但是,该 channel 似乎并没有放弃 JDBC 连接。我最终得到了 org.apache.tomcat.jdbc.pool.PoolExhaustedException。一旦 channel 为队列中的“spring.datasource.max-active”消息提供服务,就会发生这种情况。我通过使用该属性确认了这一点,它总是发生在我设置的任何数字上。在完成数据库调用后,我记录了在服务激活器中调用的服务方法,所以我知道它们已完成,但数据库连接显然永远不会交还给池...有什么想法吗?以下是我项目中的相关片段:

应用程序属性:

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>

添加异常 blah 以防有帮助:

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)

最佳答案

我想通了...感谢 Pavel Horal 对 Mark 关于“C3PO 连接池 - 连接未被释放”(以及 Mark 提出的问题)的问题的回应,这给了我一个线索我的问题以及如何解决它。结果是因为我要从 JdbcTemplate (jdbcTemplate.getDataSource().getConnection().prepareStatement(sql).execute()) 请求数据库连接;我将其替换为 jdbcTemplate.update(sql); 并且它有效!

关于tomcat - Spring Boot 不通过 spring 集成 amqp inbound-channel-adaptor 放开 tomcat.jdbc.pool 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28933492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com