Grails 2.4.5 错误 net.sf.ehcache.CacheException:javax.management.MalformedObjectNameException 属性 的值部分中的无效字符“:”

Grails 2.4.5 Error net.sf.ehcache.CacheException: javax.management.MalformedObjectNameException Invalid character ':' in value part of property

我在 运行 Grails 2.4.5 项目(完整堆栈跟踪)

时收到以下错误
Error |
2021-11-28 01:27:44,302 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener  - Error initializing the application: Error creating bean with name 'ehCacheManagementService': Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: net.sf.ehcache.CacheException: javax.management.MalformedObjectNameException: Invalid character ':' in value part of property
Message: Error creating bean with name 'ehCacheManagementService': Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: net.sf.ehcache.CacheException: javax.management.MalformedObjectNameException: Invalid character ':' in value part of property
    Line | Method
->>  266 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1149 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    624 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    748 | run       in java.lang.Thread

Caused by CacheException: net.sf.ehcache.CacheException: javax.management.MalformedObjectNameException: Invalid character ':' in value part of property
->>  246 | init      in net.sf.ehcache.management.ManagementService
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    266 | run       in java.util.concurrent.FutureTask
|   1149 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    624 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    748 | run . . . in java.lang.Thread

Caused by CacheException: javax.management.MalformedObjectNameException: Invalid character ':' in value part of property
->>   76 | createObjectName in net.sf.ehcache.management.Cache
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     64 | <init>    in     ''
|    107 | getCache  in net.sf.ehcache.management.CacheManager
|    126 | getCaches in     ''
|    237 | init . .  in net.sf.ehcache.management.ManagementService
|    266 | run       in java.util.concurrent.FutureTask
|   1149 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    624 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    748 | run . . . in java.lang.Thread

Caused by MalformedObjectNameException: Invalid character ':' in value part of property
->>  618 | construct in javax.management.ObjectName
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1382 | <init>    in     ''
|     73 | createObjectName in net.sf.ehcache.management.Cache
|     64 | <init>    in     ''
|    107 | getCache  in net.sf.ehcache.management.CacheManager
|    126 | getCaches in     ''
|    237 | init . .  in net.sf.ehcache.management.ManagementService
|    266 | run       in java.util.concurrent.FutureTask
|   1149 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    624 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    748 | run . . . in java.lang.Thread

Error |
2021-11-28 01:27:44,339 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener  - Error initializing Grails: Error creating bean with name 'ehCacheManagementService': Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: net.sf.ehcache.CacheException: javax.management.MalformedObjectNameException: Invalid character ':' in value part of property
Message: Error creating bean with name 'ehCacheManagementService': Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: net.sf.ehcache.CacheException: javax.management.MalformedObjectNameException: Invalid character ':' in value part of property
    Line | Method
->>  266 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1149 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    624 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    748 | run       in java.lang.Thread

Caused by CacheException: net.sf.ehcache.CacheException: javax.management.MalformedObjectNameException: Invalid character ':' in value part of property
->>  246 | init      in net.sf.ehcache.management.ManagementService
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    266 | run       in java.util.concurrent.FutureTask
|   1149 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    624 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    748 | run . . . in java.lang.Thread

Caused by CacheException: javax.management.MalformedObjectNameException: Invalid character ':' in value part of property
->>   76 | createObjectName in net.sf.ehcache.management.Cache
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     64 | <init>    in     ''
|    107 | getCache  in net.sf.ehcache.management.CacheManager
|    126 | getCaches in     ''
|    237 | init . .  in net.sf.ehcache.management.ManagementService
|    266 | run       in java.util.concurrent.FutureTask
|   1149 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    624 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    748 | run . . . in java.lang.Thread

Caused by MalformedObjectNameException: Invalid character ':' in value part of property
->>  618 | construct in javax.management.ObjectName
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1382 | <init>    in     ''
|     73 | createObjectName in net.sf.ehcache.management.Cache
|     64 | <init>    in     ''
|    107 | getCache  in net.sf.ehcache.management.CacheManager
|    126 | getCaches in     ''
|    237 | init . .  in net.sf.ehcache.management.ManagementService
|    266 | run       in java.util.concurrent.FutureTask
|   1149 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    624 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    748 | run . . . in java.lang.Thread

Error |
2021-11-28 01:27:44,345 [localhost-startStop-1] ERROR core.StandardContext  - Error listenerStart

Error |
2021-11-28 01:27:44,347 [localhost-startStop-1] ERROR core.StandardContext  - Context [/soctrack-web] startup failed due to previous errors

|Server running. Browse to http://localhost:8080/soctrack-web

我不确定是什么导致了错误,我得到了一个成功的“maven clean/package/install”。 但是,在执行“grails 编译”时,我从 cache-ehcache 插件中收到了一个不推荐使用的警告(如下所示),我假设这可能是我收到此错误的原因,但不确定如何解决它。

Note: C:\Users\kgeoffroy\Documents\dev\soc-track-upgrade\SOCScheduleServiceWEB\target\plugins\cache-ehcache-1.0.5\src\java\grails\plugin\cache\ehcache\G
railsEhCacheManagerFactoryBean.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

我会尝试看看是否有更高版本的插件。到目前为止,我大部分时间都被限制在一个 nexus 镜像存储库中,所以我还没有找到更高版本来测试。

我的 pom.xml

中有示例依赖项
<dependency>
      <groupId>org.grails.plugins</groupId>
      <artifactId>cache-ehcache</artifactId>
      <version>1.0.5</version>
      <type>zip</type>
    </dependency>

<dependency>
      <groupId>org.grails.plugins</groupId>
      <artifactId>hibernate4</artifactId>
      <version>4.3.8.1</version>
      <scope>runtime</scope>
      <type>zip</type>
    </dependency>
<dependency>
      <groupId>org.grails.plugins</groupId>
      <artifactId>tomcat</artifactId>
      <version>7.0.55.2</version>
      <scope>provided</scope>
      <type>zip</type>
    </dependency>

<dependency>
      <groupId>org.grails.plugins</groupId>
      <artifactId>cache</artifactId>
      <version>1.1.8</version>
      <scope>compile</scope>
      <type>zip</type>
    </dependency>
<dependency>
      <groupId>org.grails.plugins</groupId>
      <artifactId>cache-headers</artifactId>
      <version>1.1.7</version>
      <type>zip</type>
    </dependency>

<dependency>
      <groupId>org.grails.plugins</groupId>
      <artifactId>cached-resources</artifactId>
      <version>1.1</version>
      <type>zip</type>
    </dependency>


<dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>2.8.1</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

根据未解决的问题 https://github.com/grails-plugins/grails-cache-ehcache/issues/41,这可能是一个错误,但没有提供解决方案或解决方法。

我正在从 Grails 2.2.0 升级到 2.4.5,我更新了 Datasource.groovy 以指出正确的 hibernate4 ehCache class:

hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = true
    //    cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider' //Outdated class
    // http://grails-plugins.github.io/grails-cache-ehcache/guide/usage.html
    cache.region.factory_class = 'grails.plugin.cache.ehcache.hibernate.BeanEhcacheRegionFactory' // For Hibernate before 4.0
    cache.region.factory_class = 'grails.plugin.cache.ehcache.hibernate.BeanEhcacheRegionFactory4' // For Hibernate before 4.0 and higher

}

config.groovy:

/*https://github.com/grails/grails-core/releases/tag/v2.4.5
* ehcache 2.9.0 is more strict about the configuration.
* ehcache is used in the spring-security-core plugin for caching users.
* There is a problem reported as GRAILS-12120.
* As a workaround to the "net.sf.ehcache.CacheException:
* Another unnamed CacheManager already exists in the same VM.
* " error you can add this config to Config.groovy:*/
beans {
    cacheManager {
        shared = true
    }
}

我试图了解 MalformedObjectNameException: Invalid character ':' in value part of property 的来源,因为该项目没有配置任何 ehcache,它只是使用了缓存插件中的一些 classes。下面是一个示例缓存服务实现:

GrailsEhCacheCache not recognized by IntelliJ GrailsCacheManager not recognized by IntelliJ

import grails.plugin.cache.GrailsCacheManager
import grails.plugin.cache.ehcache.GrailsEhcacheCache 
import org.springframework.transaction.annotation.Transactional

class ChatCacheService
{
    static transactional = false

    static final String CHAT_CACHE_NAME = Chat.canonicalName
    static final String CHAT_HASHES_CACHE_NAME = "${CHAT_CACHE_NAME}.Hashes"

    GrailsCacheManager grailsCacheManager
    ChatMessageCacheService chatMessageCacheService
    FilterService filterService

    GrailsEhcacheCache getChatCache()
    {
        grailsCacheManager.getCache(CHAT_CACHE_NAME) as GrailsEhcacheCache
    }

    GrailsEhcacheCache getChatHashesCache()
    {
        grailsCacheManager.getCache(CHAT_HASHES_CACHE_NAME) as GrailsEhcacheCache
    }

    void addNewChat(String chatChannelName, Chat chat)
    {
        chatMessageCacheService.createChatMessageCache(chatChannelName, chatCache)
        chat.channelName = chatChannelName
        refreshChat(chatCache, chat)
    }

    void refreshChat(Chat chat)
    {
        refreshChat(chatCache, chat)
    }

    void refreshChat(GrailsEhcacheCache chatCache, Chat chat)
    {
        chat.updateChatTimestamp()
        chatCache.put(chat.channelName, chat)
        // >>> Chat Cache Event listener will update hash, except during initialization
    }

    void refreshChatHash(Chat chat)
    {
        ChatHash chatHash = chat.generateHashCode(chatMessageCacheService.getCacheChatMessages(chat.channelName))
        chatHashesCache.put(chat.channelName, chatHash)
    }

    Chat retrieveChat(String chatChannelName)
    {
        Chat chat = chatCache.get(chatChannelName)?.get() as Chat
        chat
    }

    synchronized Chat retrieveNonPurgeableChat(String chatChannelName)
    {
        Chat chat = retrieveChat(chatChannelName)
        if (!chat)
        {
            addNewChat(chatChannelName, new NonPurgeableChat())
            chat = retrieveChat(chatChannelName)
        }
        chat
    }

    List<ChatChatMessage> getCacheChatMessages(String channelName)
    {
        chatMessageCacheService.getCacheChatMessages(channelName) //.sort { -it.id }
    }

    ChatChatMessage addNewChatMessageCache(String channelName, ChatMessage chatMessage)
    {
        Chat chat = retrieveChat(channelName)
        chatMessageCacheService.addNewChatMessageCacheToTop(channelName, chatMessage)
        refreshChat(chat) // refresh chat
    }

    void removeChat(String chatChannelName)
    {
        GrailsEhcacheCache chatCache = grailsCacheManager.getCache(CHAT_CACHE_NAME) as GrailsEhcacheCache
        chatCache.evict(chatChannelName)
        chatMessageCacheService.destroy(chatChannelName)
    }

    @Transactional(readOnly = true)
    List<Chat> retrieveAllGeneralAndGroupChats()
    {
        List<Chat> generalAndGroupChats = []
        List<ChatMessageType> generalAndGroupChatTypes = [
                ChatMessageType.findByDescription(ChatType.GROUP.name()),
                ChatMessageType.findByDescription(ChatType.GENERAL.name())
        ]
        chatCache.allKeys.each { key ->
            Chat chat = (Chat) chatCache.get(key).get()
            if (generalAndGroupChatTypes.find { it?.description == chat?.chatTypeDescription })
            {
                generalAndGroupChats.add(chat)
            }
        }
        generalAndGroupChats
    }

    void purgeOldMessagesFromChats()
    {
        chatCache.allKeys.findAll { String channelName -> !ChatService.isEventChat(channelName) }.each { String chatChannelName ->
            Chat chat = (Chat) chatCache.get(chatChannelName).get()
            if (!(chat instanceof NonPurgeableChat))
            {
                if (chatMessageCacheService.purgeOldMessages(chatChannelName) > 0)
                {
                    refreshChat(chatCache, chat)
                }
            }
        }
    }

    String getChatCacheHash(String channelName)
    {
        ((ChatHash) chatHashesCache.get(channelName)?.get())?.hash
    }

    String synchronizeChat(UserProfile userProfile, String chatChannelName, String chatTimestamp, String selectedFilter)
    {
        String response = null
        Chat chatToSynchronize = retrieveChat(chatChannelName)
        if (chatToSynchronize && (chatTimestamp != getChatCacheHash(chatToSynchronize.channelName) || filterChanged(userProfile, selectedFilter)))
        {
            response = chatToJson(chatToSynchronize, filterService.getCurrentFilterForUsername(userProfile?.userId), selectedFilter, userProfile)
        }
        response
    }

    Boolean filterChanged(UserProfile userProfile, String selectedFilter) {
        Boolean filterChanged = Boolean.FALSE

        if (userProfile && userProfile?.groupChatFilterOption != selectedFilter
                && (userProfile?.groupChatFilterOption == SOCTrackConstants.DASHBOARD_FILTER || selectedFilter == SOCTrackConstants.DASHBOARD_FILTER)) {
            filterChanged = Boolean.TRUE
        }

        filterChanged
    }

    String chatToJson(Chat chat, List<EventFilter> userFilters, String selectedFilter, UserProfile userProfile)
    {
        List<ChatChatMessage> chatChatMessages = chatMessageCacheService.getCacheChatMessages(chat.channelName)

        // Apply dashboard filters
        if (selectedFilter == SOCTrackConstants.DASHBOARD_FILTER) {
            chatChatMessages = chatChatMessages.findAll{ChatChatMessage chatMessage ->
                includeChatMessage(chatMessage, userFilters)
            }
        }

        Map<String, Collection<String>> chatMessagesAndTimestamp = [:]
        chatMessagesAndTimestamp['messages'] = chatChatMessages.collect { ChatChatMessage chatMessage ->
            chatMessage.messageAsHtml = chatMessage.messageAsHtmlClone
            if (! userProfile?.shouldHighlightGroupMessage(chatMessage.id, chatMessage.enteredDate, chatMessage.messageType) ||
                    chatMessage.enteredBy.equals(userProfile?.userId)) {
                chatMessage.messageAsHtml = chatMessage.messageAsHtml.replaceAll("groupChatUnreadHighlight","")
            }
            if(chatMessage.lastResponseId > 0) {
                if (!userProfile?.shouldHighlightGroupMessage(chatMessage.lastResponseId, chatMessage.lastResponseDate, chatMessage.messageType) ||
                        chatMessage.lastResponseBy.equals(userProfile?.userId)) {
                    chatMessage.messageAsHtml = chatMessage.messageAsHtml.replaceAll("groupChatResponseUnreadHighlight", "")
                }
            }
            chatMessage.messageAsHtml
        }.findAll { it }
        chatMessagesAndTimestamp['chatTimestamp'] = getChatCacheHash(chat.channelName) // chat.chatTimestamp
        JSON messagesAsJson = new JSON(chatMessagesAndTimestamp)
        messagesAsJson.toString()
    }

    Boolean includeChatMessage (ChatChatMessage chatChatMessage, List<EventFilter> userFilters) {
        boolean displayRow = true
        if (userFilters) { //at least one filter to satisfy
            boolean satisfiedAllAndFilters
            boolean satisfiedAnOrFilter

            List<EventFilter> andFilters = userFilters.findAll { eventFilter ->
                eventFilter.filterOperationType == FilterOperationType.AND
            }
            List<EventFilter> orFilters = userFilters.findAll { eventFilter ->
                eventFilter.filterOperationType == FilterOperationType.OR
            }

            if (andFilters) {
                satisfiedAllAndFilters = andFilters.collect { andFilter ->
                    andFilter.displayChatBasedOnFilter(chatChatMessage)
                }.every{ it } //all 'AND' filters must be satisfied
            }

            if (orFilters) {
                satisfiedAnOrFilter = orFilters.collect { orFilter ->
                    orFilter.displayChatBasedOnFilter(chatChatMessage)
                }.any{ it } //at least one 'OR' filter must be satisfied
            }

            displayRow = satisfiedAllAndFilters || satisfiedAnOrFilter
        }

        displayRow
    }
}

更新:用于创建 EHcache 的自定义实用程序方法

synchronized static Cache createEhcache(String name, CacheManager cacheManager, CacheConfiguration baseCacheConfiguration, CacheEventListener cacheEventListener = null)
    {
        Ehcache ehcache
        PersistenceConfiguration persistenceConfiguration

        if (cacheManager.cacheExists(name))
        {
            ehcache = cacheManager.getCache(name)
            ehcache.cacheConfiguration.overflowToDisk = false //overflowToDisk deprecated
//            ehcache.cacheConfiguration.persistence(persistenceConfiguration.strategy("LOCALTEMPSWAP"))
            ehcache.cacheConfiguration.diskPersistent = false // diskPersistent deprecated
//            ehcache.cacheConfiguration.persistence(persistenceConfiguration.strategy("NONE"))
            ehcache.cacheConfiguration.timeToLiveSeconds = 0
            ehcache.cacheConfiguration.timeToIdleSeconds = 0
            logger.warn "Cache $name already exists, configuration has been reset."
        }
        else
        {
            baseCacheConfiguration.name = name
//            ehcache = new Cache(baseCacheConfiguration, null, null); //Runtime error (Ambiguous method overloading)
//            RegisteredEventListeners  registeredEventListeners = new RegisteredEventListeners(null);
//            BootstrapCacheLoader bootstrapCacheLoader = new RMIBootstrapCacheLoader()
            ehcache = new Cache(baseCacheConfiguration, null as RegisteredEventListeners, null as BootstrapCacheLoader)
            cacheManager.addCache(ehcache)
            cacheManager.cacheManagerPeerProviders?.get('RMI')?.registerPeer("${getEhcachePeerBaseUrl()}/$baseCacheConfiguration.name")
            if (cacheEventListener)
            {
                ehcache.getCacheEventNotificationService().registerListener(cacheEventListener)
            }
        }
        ehcache
    }

将该行代码放入 Config.groovy 文件解决了问题并删除了异常:grails.cache.ehcache.cacheManagerName = 'default_grails_cache'.

解决方案来自https://github.com/grails-plugins/grails-cache-ehcache/issues/41#issuecomment-985049476.