Search in sources :

Example 1 with UnknownForkHandler

use of org.jgroups.fork.UnknownForkHandler in project wildfly by wildfly.

the class JChannelFactory method createChannel.

@Override
public JChannel createChannel(String id) throws Exception {
    FORK fork = new FORK();
    fork.enableStats(this.configuration.isStatisticsEnabled());
    fork.setUnknownForkHandler(new UnknownForkHandler() {

        private final short id = ClassConfigurator.getProtocolId(RequestCorrelator.class);

        @Override
        public Object handleUnknownForkStack(Message message, String forkStackId) {
            return this.handle(message);
        }

        @Override
        public Object handleUnknownForkChannel(Message message, String forkChannelId) {
            return this.handle(message);
        }

        private Object handle(Message message) {
            Header header = (Header) message.getHeader(this.id);
            // If this is a request expecting a response, don't leave the requester hanging - send an identifiable response on which it can filter
            if ((header != null) && (header.type == Header.REQ) && header.rspExpected()) {
                Message response = message.makeReply().setFlag(message.getFlags()).clearFlag(Message.Flag.RSVP, Message.Flag.INTERNAL);
                response.putHeader(FORK.ID, message.getHeader(FORK.ID));
                response.putHeader(this.id, new Header(Header.RSP, header.req_id, header.corrId));
                response.setBuffer(UNKNOWN_FORK_RESPONSE.array());
                fork.getProtocolStack().getChannel().down(response);
            }
            return null;
        }
    });
    Map<String, SocketBinding> bindings = new HashMap<>();
    // Transport always resides at the bottom of the stack
    List<ProtocolConfiguration<? extends Protocol>> transports = Collections.singletonList(this.configuration.getTransport());
    // Add RELAY2 to the top of the stack, if defined
    List<ProtocolConfiguration<? extends Protocol>> relays = this.configuration.getRelay().isPresent() ? Collections.singletonList(this.configuration.getRelay().get()) : Collections.emptyList();
    List<Protocol> protocols = new ArrayList<>(transports.size() + this.configuration.getProtocols().size() + relays.size() + 1);
    for (List<ProtocolConfiguration<? extends Protocol>> protocolConfigs : Arrays.asList(transports, this.configuration.getProtocols(), relays)) {
        for (ProtocolConfiguration<? extends Protocol> protocolConfig : protocolConfigs) {
            protocols.add(protocolConfig.createProtocol(this.configuration));
            bindings.putAll(protocolConfig.getSocketBindings());
        }
    }
    // Add implicit FORK to the top of the stack
    protocols.add(fork);
    // Override the SocketFactory of the transport
    protocols.get(0).setSocketFactory(new ManagedSocketFactory(this.configuration.getSocketBindingManager(), bindings));
    JChannel channel = new JChannel(protocols);
    channel.setName(this.configuration.getNodeName());
    TransportConfiguration.Topology topology = this.configuration.getTransport().getTopology();
    if (topology != null) {
        channel.addAddressGenerator(new TopologyAddressGenerator(topology));
    }
    return channel;
}
Also used : SocketBinding(org.jboss.as.network.SocketBinding) JChannel(org.jgroups.JChannel) FORK(org.jgroups.protocols.FORK) Message(org.jgroups.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TransportConfiguration(org.wildfly.clustering.jgroups.spi.TransportConfiguration) UnknownForkHandler(org.jgroups.fork.UnknownForkHandler) ProtocolConfiguration(org.wildfly.clustering.jgroups.spi.ProtocolConfiguration) Header(org.jgroups.blocks.RequestCorrelator.Header) Protocol(org.jgroups.stack.Protocol)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 SocketBinding (org.jboss.as.network.SocketBinding)1 JChannel (org.jgroups.JChannel)1 Message (org.jgroups.Message)1 Header (org.jgroups.blocks.RequestCorrelator.Header)1 UnknownForkHandler (org.jgroups.fork.UnknownForkHandler)1 FORK (org.jgroups.protocols.FORK)1 Protocol (org.jgroups.stack.Protocol)1 ProtocolConfiguration (org.wildfly.clustering.jgroups.spi.ProtocolConfiguration)1 TransportConfiguration (org.wildfly.clustering.jgroups.spi.TransportConfiguration)1