-更新-
事实证明,一段时间后堆将被清空。但是,线程数只是无止境地增长。在具有8Gb RAM的mac上,我很好,但是在具有1Gb RAM的生产机上,我得到了:



我确实使用Spring Boot(1.2.7.RELEASE)和Apache Camel(2.15.0)编写了一个简单的应用程序。该应用程序很简单,只有一条路由:计时器将每1秒钟在Bean上调用一个方法。调用的方法将使用ProducerTemplate ssh进入远程计算机,执行一个小的脚本,然后将输出打印到控制台。简单吧?
但是,在对此进行概要分析时,我可以看到线程数,并且堆穿过了屋顶!似乎为ssh创建的任何线程都不会被杀死,而是停放。因此,我可以很快地运行OOM。
让我向您展示一些探查器输出:
spring - Spring-Boot + Camel + producerTemplate =数千个线程-LMLPHP
spring - Spring-Boot + Camel + producerTemplate =数千个线程-LMLPHP
spring - Spring-Boot + Camel + producerTemplate =数千个线程-LMLPHP

如您所见,线程/堆快速上升。
该应用程序代码很少,因此我将在此处提供所有代码供参考。
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("----");
    }
}

如您所见,该应用程序非常小巧,并且可以正常运行(我可以在运行该应用程序的控制台中看到远程脚本的输出)。但是就像我说的,它像新鲜的 cookies 一样吃掉内存!
现在我确实读过this。但是,在我的应用程序中,ProducerTemplate是由Camelcontext本身实例化的bean。因此我无法使用producerTemplate.stop(),因为下一个触发器将引发异常,提示模板未启动...
所以我的主要问题是:我是否以错误的方式使用了ProducerTemplate?如果可以,该如何使用?
如果我没有做错任何事情,那是一个错误吗?我应该举报吗?

最佳答案

如原始海报所述:

09-26 22:55