Code example
Disruptor server
package com.gbcom.frame.disruptor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import com.lmax.disruptor.BlockingWaitStrategy; import com.lmax.disruptor.EventTranslatorThreeArg; import com.lmax.disruptor.RingBuffer; import com.lmax.disruptor.dsl.Disruptor; import com.lmax.disruptor.dsl.ProducerType; /** * DisruptorServer, which encapsulates the Disruptor service, provides start, stop, registered consumers, and provides functions such as publishing methods * * * This class can be used directly and is independent of business. This class describes the core of the Disruptor * @author syz * @date 2014-10-17 * @version v1.0.0 * @see com.gbcom.frame.disruptor.DisruptorServer */ public class DisruptorServer { private static final int BUFFER_SIZE = 4; private Disruptor<FrameEvent> disruptor; private EventProcessor processor = new EventProcessor(); /** * Data encapsulation; encapsulated into FrameEvent */ private static final EventTranslatorThreeArg<FrameEvent, String, Integer, byte[]> TRANSLATOR = new EventTranslatorThreeArg<FrameEvent, String, Integer, byte[]>(){ @Override public void translateTo(FrameEvent event, long seq, String ip, Integer port, byte[] data) { event.setData(data); event.setIp(ip); event.setPort(port); } }; private static class DisruptorHolder{ private static final DisruptorServer INSTANCE = new DisruptorServer(); } public static DisruptorServer getInstance(){ return DisruptorHolder.INSTANCE; } public void start(){ initDisruptor(); } private void initDisruptor() { ExecutorService executor = Executors.newCachedThreadPool(); //Thread pool, needed to build disruptor disruptor = new Disruptor<FrameEvent>(FrameEvent.ENENT_FACTORY, BUFFER_SIZE, executor, ProducerType.SINGLE, new BlockingWaitStrategy());//构建disruptor disruptor.handleEventsWith(new FrameEventHandler<FrameEvent>(processor));//Register a consumer, the consumer is registered in advance disruptor.start();//Start concurrent service disruptor.getRingBuffer();//Start rotation, the size of rotation is specified in the construction of disruptor, based on this mechanism to achieve lock-free high concurrency } /** * Producer, publish a message: (DisruptorServer.onData) * @paramip * @param port * @param data */ public void onData(String ip,Integer port,byte[] data){ disruptor.getRingBuffer().publishEvent(TRANSLATOR,ip,port,data); } /** * Handling of production errors. Called directly, not added to the queue. * @param t */ public void onError(Throwable t){ if(processor !=null){ processor.onError(t); } } }
Events of the disruptor framework
package com.gbcom.frame.disruptor; import java.util.Arrays; import com.lmax.disruptor.EventFactory; /** * Events of the disruptor framework. POJO, disruptor needs to provide factory, * @author syz * @date 2014-10-17 * @version v1.0.0 * @see com.gbcom.frame.disruptor.FrameEvent */ public class FrameEvent { private byte[] data; private String ip; private int port; public byte[] getData() { return data; } public void setData(byte[] data) { this.data = data; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } /** * Factory class */ public static final EventFactory<FrameEvent> ENENT_FACTORY = new EventFactory<FrameEvent> (){ @Override public FrameEvent newInstance() { // TODO Auto-generated method stub return new FrameEvent(); } }; @Override public String toString() { return "FrameEvent [data=" + Arrays.toString(data) + ", ip=" + ip + ", port=" + port + "]"; } }
FrameEventHandler implements the EventHandler interface, consumer processing
package com.gbcom.frame.disruptor; import com.lmax.disruptor.EventHandler; /** * Consumer processing, implements the EventHandler interface * @author SYZ * @date 2016-11-3 02:08:32 pm * @version 1.0.0 * @see com.gbcom.frame.disruptor.FrameEventHandler */ public class FrameEventHandler<FrameEvent> implements EventHandler<FrameEvent>{ private EventProcessor processor ; protected FrameEventHandler(EventProcessor processor) { super(); this.processor = processor; } @Override public void onEvent(FrameEvent event, long seq, boolean endOfBatch) throws Exception { processor.onReceive((com.gbcom.frame.disruptor.FrameEvent) event); } }
Consumer: real message processing
package com.gbcom.frame.disruptor; import java.io.UnsupportedEncodingException; import org.apache.commons.io.Charsets; import com.gbcom.frame.disruptor.snetty.UdpHandler; /** * Consumer: real message processing * @author syz * @date 2014-10-17 * @version v1.0.0 * @see com.gbcom.frame.disruptor.EventProcessor */ public class EventProcessor implements UdpHandler{ public void onReceive(FrameEvent event){ System.out.println("receive is success :"+event); try { NatTabManager.getInstance().put(new String(event.getData(),"utf-8"), event); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace (); } event = NatTabManager.getInstance().getNat(new String(event.getData(),Charsets.UTF_8)); //System.out.println(HttpClientUtil.get("http://"+event.getIp()+":"+event.getPort())); } public void onError(Throwable t ){ System.out.println("receive is false :" + t); } public static void main(String args[]){ System.out.println(HttpClientUtil.get("http://127.0.0.1:80")); } @Override public void onReceive(String ip, int port, byte[] data) { } }
NET function, has nothing to do with the Disruptor framework, the upper application
package com.gbcom.frame.disruptor; import java.util.HashMap; import java.util.Map; /** * NET function, independent of the Disruptor framework, * @author SYZ * @date 2016-11-3 02:09:14 PM * @version 1.0.0 * @see com.gbcom.frame.disruptor.NatTabManager */ public class NatTabManager { private Map<String,FrameEvent> natTab = new HashMap<String,FrameEvent>(); private static NatTabManager intance = new NatTabManager(); public static NatTabManager getInstance(){ return intance; } public void put(String key,FrameEvent value){ natTab.put(key, value); } public Map getNatTab(){ return natTab; } public FrameEvent getNat(String key){ return natTab.get(key); } }
client
package com.gbcom.frame.disruptor; /** * * * Disruptor: An efficient concurrent component (framework), which can be considered as a producer-consumer model without locks, so it is highly efficient * * This example completes the Disruptor function and has a certain use value. Disruptor is independent of business,,, DisruptorServer * * Efficient producer-consumer model: DisruptorServer * * @author SunYanzheng * @date 2014-10-17 * @version v1.0.0 * @see com.gbcom.frame.disruptor.DisClient */ public class DisClient { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DisruptorServer.getInstance().start(); //Initialize the service DisruptorServer.getInstance().onData("10.1.1.1", 162, "hello".getBytes());//Call onData to publish data } }
Complete code download reference attachment