You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java应用中使用Liberty的distributedMap特性及获取Maven依赖

在Java应用中使用Liberty的distributedMap特性及Maven依赖配置

一、使用distributedMap的步骤

1. 配置Liberty服务器特性

首先在Liberty的server.xml中启用分布式Map相关特性,并定义具体的distributedMap实例:

<server>
    <featureManager>
        <!-- 启用distributedMap核心特性 -->
        <feature>distributedMap-1.0</feature>
        <!-- 若需要集群分布式缓存,添加集群成员特性 -->
        <feature>clusterMember-1.0</feature>
        <!-- 若使用CDI注入,需启用CDI特性 -->
        <feature>cdi-2.0</feature>
    </featureManager>

    <!-- 定义一个分布式Map实例,id和name可自定义 -->
    <distributedMap id="myAppCache" name="appGlobalCache"/>
</server>

2. 在Java代码中使用distributedMap

有两种常用方式获取distributedMap实例:

方式1:JNDI查找

通过JNDI路径查找配置好的实例:

import javax.naming.InitialContext;
import com.ibm.websphere.cache.DistributedMap;

public class CacheUtil {
    private static DistributedMap cache;

    static {
        try {
            InitialContext ctx = new InitialContext();
            // JNDI路径格式:java:comp/env/distributedMap/[配置的id]
            cache = (DistributedMap) ctx.lookup("java:comp/env/distributedMap/myAppCache");
        } catch (Exception e) {
            throw new RuntimeException("Failed to initialize distributed map", e);
        }
    }

    public static void put(String key, Object value) {
        cache.put(key, value);
    }

    public static Object get(String key) {
        return cache.get(key);
    }

    public static void remove(String key) {
        cache.remove(key);
    }
}

方式2:CDI注入

若启用了CDI特性,可通过@Resource注解直接注入:

import javax.annotation.Resource;
import com.ibm.websphere.cache.DistributedMap;
import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class CacheService {
    // 直接通过lookup指定JNDI路径注入
    @Resource(lookup = "java:comp/env/distributedMap/myAppCache")
    private DistributedMap appCache;

    public void saveData(String key, Object data) {
        appCache.put(key, data);
    }

    public Object retrieveData(String key) {
        return appCache.get(key);
    }
}

二、获取Maven依赖

1. 配置IBM Maven仓库

在pom.xml中添加IBM官方提供的Maven仓库,用于拉取Liberty相关API依赖:

<repositories>
    <repository>
        <id>ibm-wasdev-repo</id>
        <name>IBM WASdev Repository</name>
        <url>https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/maven/repository/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

2. 添加distributedMap API依赖

添加com.ibm.websphere.appserver.api.distributedMap依赖,注意scope设为provided,因为Liberty运行时会自带该类库,无需打包到应用WAR中:

<dependencies>
    <dependency>
        <groupId>com.ibm.websphere.appserver.api</groupId>
        <artifactId>com.ibm.websphere.appserver.api.distributedMap</artifactId>
        <!-- 可根据Liberty版本选择对应版本号,如1.0.51对应较新的Liberty版本 -->
        <version>1.0.51</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

内容的提问来源于stack exchange,提问作者Gualtiero Testa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 19:45:34