All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.openhft.chronicle.map.channel.MapHandler Maven / Gradle / Ivy

package net.openhft.chronicle.map.channel;

import net.openhft.affinity.AffinityLock;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.channel.internal.MapChannel;
import net.openhft.chronicle.wire.channel.AbstractHandler;
import net.openhft.chronicle.wire.channel.ChronicleChannel;
import net.openhft.chronicle.wire.channel.ChronicleChannelCfg;
import net.openhft.chronicle.wire.channel.ChronicleContext;

import java.io.IOException;

public class MapHandler extends AbstractHandler> {
    protected MapService mapService;
    private String mapName;

    protected MapHandler(String mapName) {
        this.mapName = mapName;
    }

    public static  MapHandler createMapHandler(String mapName, MapService mapService) {
        MapHandler mh = new MapHandler<>(mapName);
        mh.mapService = mapService;
        return mh;
    }

    @Override
    public void run(ChronicleContext context, ChronicleChannel channel) {
        try (ChronicleMap, VALUE> map = MapChannel.createMap(mapName, mapService, context)) {
            REPLY REPLY = channel.methodWriter(mapService().replyClass());
            mapService.map(map);
            mapService.reply(REPLY);
            try (AffinityLock lock = context.affinityLock()) {
                channel.eventHandlerAsRunnable(mapService).run();
            }

        } catch (IOException ioe) {
            throw Jvm.rethrow(ioe);
        }
    }

    @Override
    public ChronicleChannel asInternalChannel(ChronicleContext context, ChronicleChannelCfg channelCfg) {
        return new MapChannel(mapName, mapService, context, channelCfg);
    }

    protected MapService mapService() {
        return mapService;
    }
}




© 2015 - 2026 Weber Informatics LLC | Privacy Policy