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

org.jgroups.protocols.DROP Maven / Gradle / Ivy

Go to download

This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up with different versions on classes on the class path).

The newest version!
package org.jgroups.protocols;

import org.jgroups.Message;
import org.jgroups.annotations.MBean;
import org.jgroups.stack.Protocol;
import org.jgroups.util.MessageBatch;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;

/**
 * Protocol which drops up or down messages according to user-defined filters
 * @author Bela Ban
 * @since  4.0.1
 */
@MBean(description="Drops up or down messages according to user-defined filters")
public class DROP extends Protocol {
    protected final List> down_filters=new ArrayList<>(), up_filters=new ArrayList<>();

    public DROP addDownFilter(Predicate filter) {
        down_filters.add(filter);
        return this;
    }

    public DROP addUpFilter(Predicate filter) {
        up_filters.add(filter);
        return this;
    }

    public DROP removeDownFilter(Predicate filter) {
        down_filters.remove(filter);
        return this;
    }

    public DROP removeUpFilter(Predicate filter) {
        up_filters.remove(filter);
        return this;
    }

    public DROP clearUpFilters()   {up_filters.clear(); return this;}
    public DROP clearDownFilters() {down_filters.clear(); return this;}


    public Object down(Message msg) {
        for(Predicate pred: down_filters)
            if(pred.test(msg)) {
                dropped(msg, true);
                return null;
            }
        return down_prot.down(msg);
    }

    public Object up(Message msg) {
        for(Predicate pred: up_filters)
            if(pred.test(msg)) {
                dropped(msg, false);
                return null;
            }
        return up_prot.up(msg);
    }

    public void up(MessageBatch batch) {
        for(Iterator it=batch.iterator(); it.hasNext();) {
            Message msg=it.next();
            for(Predicate pred: up_filters) {
                if(pred.test(msg)) {
                    dropped(msg, false);
                    it.remove();
                    break;
                }
            }
        }
        if(!batch.isEmpty())
            up_prot.up(batch);
    }

    protected void dropped(Message msg, boolean down) {
        log.trace("dropped msg %s hdrs: %s\n", down? "to " + msg.getDest() : "from " + msg.getSrc(), msg.printHeaders());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy