2016-12-28 77 views
0

我正在嘗試爲JMX配置EhCache。看起來我只需要使用EhCacheManager並將其註冊爲mBeanServer。如何在Spring Boot中註冊對EhCache的JMX支持?

這裏是我的CONFIGS:

@Configuration 
public class EhcacheConf { 

    @Bean 
    public CacheManager getEhCacheManager() { 
     return getEhCacheFactory().getObject(); 
    } 

    public EhCacheManagerFactoryBean getEhCacheFactory() { 
     EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean(); 
     factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml")); 
     factoryBean.setShared(true); 
     return factoryBean; 
    } 

    @Bean 
    public ManagementService managementService() { 
     return new ManagementService(getEhCacheManager(), 
       mbeanServer(), 
       true, 
       true, 
       true, 
       true, 
       true); 
    } 

    @Bean 
    public MBeanServer mbeanServer() { 
     MBeanServerFactoryBean mBeanServerFactoryBean = new MBeanServerFactoryBean(); 
     mBeanServerFactoryBean.setLocateExistingServerIfPossible(true); 
     return mBeanServerFactoryBean.getObject(); 
    } 

} 

application.yaml

spring: 
    datasource: 
     url: "jdbc:h2:file:~/testdb" 
     username: sa 
     password: sa 
     driver-class-name: org.h2.Driver 
    jpa: 
     hibernate: 
     ddl-auto: create-drop 
    cache: 
     type: ehcache 
     ehcache: 
     config: classpath:ehcache.xml 

endpoints: 
    jmx: 
    domain: quiz 
    unique-names: true 

ehcache.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" 
     updateCheck="false" 
     monitoring="autodetect" 
     dynamicConfig="true"> 

    <cache name="Folder" 
      maxEntriesLocalHeap="10000" 
      maxEntriesLocalDisk="1000" 
      eternal="false" 
      diskSpoolBufferSizeMB="20" 
      timeToIdleSeconds="300" timeToLiveSeconds="600" 
      memoryStoreEvictionPolicy="LFU" 
      transactionalMode="off"> 
     <persistence strategy="localTempSwap"/> 
    </cache> 

</ehcache> 

的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>com.example</groupId> 
    <artifactId>demo</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>demo</name> 
    <description>Demo project for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.3.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-rest</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>com.h2database</groupId> 
      <artifactId>h2</artifactId> 
     </dependency> 

     <!-- Caching --> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-cache</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>net.sf.ehcache</groupId> 
      <artifactId>ehcache</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.projectlombok</groupId> 
      <artifactId>lombok</artifactId> 
      <optional>true</optional> 
     </dependency> 

    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

有誰知道什麼是錯的?

感謝

回答

5

首先,你並不需要所有的東西配置了Ehcache 2.X和春天開機。您所需要的只是@EnableCaching ehcache依賴關係和您的ehcache.xml配置文件在通常位置。

您的配置也覆蓋了MBeanServer,它關閉了Spring Boot在那裏爲您做的事情。

無論如何,一個樣本總值千字,所以I've created one for you

編輯:由於Timur Milovanov提到這個示例沒有在關閉時取消註冊MBean,我已經更新了示例以解決此問題。

+0

謝謝你的幫助!樣品真棒! –

+0

來自Nicoll的示例在上下文停止時不取消註冊mbean。 一個靜態方法javadoc:_A一個方便的靜態方法,它創建...這個提供了向後兼容性_ 定義bean並使用'ManagementService'的構造函數可能會更好。在bean定義中,你應該定義init/destroy方法:'@Bean(initMethod =「init」,destroyMethod =「dispose」)'。這個'ManagementService#dispose'方法從MBeanServer註銷與緩存相關的實例。 PS:如果在沒有容器重啓的情況下在容器(tomcat,jetty)中部署/取消部署應用程序,取消註冊可能很重要 –

+1

非常感謝@TimurMilovanov,我更新了示例。 –

相關問題