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

Posted

技术标签:

【中文标题】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 【发布时间】:2022-01-05 13:18:15 【问题描述】:

在运行 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 compile”时,我从 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 类:

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,它只是使用了缓存插件中的一些类。下面是一个示例缓存服务实现:

GrailsEhCacheCache not recognized by IntelliJGrailsCacheManager 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.findAllChatChatMessage 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
    

【问题讨论】:

"我试图了解 MalformedObjectNameException: Invalid character ':' in value part of property 的来源,因为项目没有配置任何 ehcache" - 它可能来自 @987654333 @互动github.com/grails-plugins/grails-cache-ehcache/blob/…。 修复它的最佳方法是什么? @杰夫斯科特布朗 我作为更新添加了用于创建 EhCache(s) @JeffScottBrown 的自定义实用程序方法 “修复它的最佳方法是什么?” - 我不确定。您正在使用 3 个主要版本之前且大约 7 年历史的 Grails 版本。如果升级是一种选择,那可能会这样做。您也可以尝试为 grails.cache.ehcache.cachManagerName 分配一个值,这样我上面链接的有问题的表达式就不会成为问题。 我在上一条评论中提出的建议只有在我上面链接的NavigableMap 交互是真正的罪魁祸首时才会有所帮助。我还没有证实它是,但它看起来像一个有前途的嫌疑人。祝你好运! 【参考方案1】:

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

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

【讨论】:

以上是关于Grails 2.4.5 错误 net.sf.ehcache.CacheException:javax.management.MalformedObjectNameException 属性值部分中的无的主要内容,如果未能解决你的问题,请参考以下文章

Grails 从 2.4.5 升级到 3.3.2:关系“xxx”不存在

Grails从2.4.5升级到3.3.2:关系'xxx'不存在

Grails 2.5.1 domain.get(id)获取缓存数据

Grails从1.3升级到2.4

在 Grails 中注销后,浏览器后退按钮显示上一页

Grails - 在刷新 grails 错误之前保存瞬态实例?