--- --- UPDATE
事实证明,堆在一段时间后会被清空。但是线程的数量只是增长而没有结束。在我的Mac上使用8Gb RAM我很好,但是在1Gb的生产机器上我得到了:
线程“Thread-341”中的异常java.lang.OutOfMemoryError:无法创建新的本机线程
我确实使用Spring Boot(1.2.7.RELEASE)和Apache Camel(2.15.0)编写了一个简单的应用程序。应用程序很简单,只有1个路由:计时器将每隔1秒调用一个bean上的方法。调用的方法将使用ProducerTemplate
ssh到远程计算机,执行一个小脚本,并将输出打印到控制台。简单,对吧?
但是,在分析这个时,我可以看到线程的数量,并且堆通过屋顶!似乎为ssh创建的任何线程都不会被杀死,而是停放。因此,我很快就运行了OOM
让我向您展示一些分析器输出:
正如您所见,线程/堆快速上升。 应用程序代码很少,因此我将在此提供所有内容以供参考 的的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tests</groupId>
<artifactId>camel-producer-template-testing</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<start-class>app.Application</start-class>
<camel.version>2.15.0</camel.version>
<spring-boot.version>1.2.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ftp</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ssh</artifactId>
<version>${camel.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Application.java:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.TimeZone;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
SpringApplication application = new SpringApplication(Application.class);
application.run(args);
}
}
MyAppContext.java:
import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.SpringCamelContext;
import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("application.properties")
public class MyAppContext {
private final String sshKeyPath = "/Users/gruszd/.ssh/id_rsa";
@Autowired
private ApplicationContext applicationContext;
@Bean
public CamelContext camelContext() {
return new SpringCamelContext(applicationContext);
}
@Bean
FileKeyPairProvider keyPairProvider() {
return new FileKeyPairProvider(new String[]{sshKeyPath});
}
@Bean
RoutesBuilder myRouter() {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("timer://foo?period=1000").to("bean:sftpStager?method=stage");
}
};
}
}
SftpStager.java:
import org.apache.camel.ProducerTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SftpStager {
@Autowired
private ProducerTemplate producerTemplate;
public void stage() throws Exception {
String response = producerTemplate.requestBody(
"ssh://_remote.machine.url.here_?username=_username_&keyPairProvider=#keyPairProvider",
"/home/_username_/some_temp_script.sh",
String.class);
System.out.println("----");
System.out.println(response);
System.out.println("----");
}
}
正如您所看到的那样,应用程序非常简单,并且可以正常运行(我可以在运行应用程序的控制台中看到远程脚本的输出)。但就像我说的那样,它会像新鲜的饼干一样吃掉记忆!
现在我读过this。但是,在我的应用程序中,ProducerTemplate
是由Camelcontext
本身实例化的bean。因此我不能producerTemplate.stop()
因为下一个触发器会抛出一个异常,说模板没有启动...
所以我的主要问题是:我是否以错误的方式使用ProducerTemplate
?如果我这样做,我应该如何使用它?
如果我没有做错什么,这是一个错误吗?我应该报告吗?
答案 0 :(得分:0)
如原始海报所述:
原来这是Apache Camel本身的一个错误,应该[并且]在2.16.2中修复:Jira Issue here
答案 1 :(得分:0)
您必须停止/清除producerTemplate的状态。
有producerTemplate.stop()
之类的内置方法,或者由于您已经自动连接了Producer模板,因此您可以尝试producerTemplate.cleanUp()