Apache CXF list of uncorrelatedExchanges grows until memory is exhausted

53 Views Asked by At

I have a long-running CXF SOAP connection to a physical device. Over time, I see the org.apache.cxf.ws.addressing.soap.MAPCodec object retaining heap. It appears that the ConcurrentHashpMap uncorrelatedExchanges is growing and the entries in the map are not being cleared. After only 12 hours of running, this map now contains almost 54,000 entries. Is there a way I can clear out the uncorrelatedExchanges? I am using CXF 3.4.0.

Thanks, David

1

There are 1 best solutions below

0
7er On

Same here, the workaround is to use Weak keys in uncorrelatedExchanges... With https://guava.dev/

    public static <T> boolean setPrivateFinalField(Class<T> clazz, String fieldName, T instance, Object value) {
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
            field.set(instance, value);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }

        ....
        WSAddressingFeature wsAddressingFeature = new WSAddressingFeature();
        wsAddressingFeature.initialize(client, client.getBus());
        client.getInInterceptors().stream().filter(MAPCodec.class::isInstance).findAny().map(MAPCodec.class::cast).ifPresent(mapCodec -> {
            // OOM workaround on no-relates-to responses
            Map<Object, Object> weakMap = new MapMaker().weakKeys().weakValues().makeMap();
            setPrivateFinalField(MAPCodec.class, "uncorrelatedExchanges", mapCodec, weakMap);
        });