יום שלישי, 6 במאי 2014

Simple 0MQ request replay client and server

The biggest problem of 0MQ in my opinion is it’s installation process.
In contradiction to Rabbit , MSMQ and ActiveMQ its very hard to install it and use it.
But there are a lot of samples in any language.
The project:
Capture50
The main

package test0mq2;
import java.lang.reflect.Field;
public class Test0Mq2 {
    public static void main(String[] args) throws Exception {
 
        SetLibraryPath();
 
        StartServer();
        
        SendMsgToServer();
       
    }
    private static void StartServer()
    {
        ZeroMQRepServer theZeroMQRepServer = new ZeroMQRepServer();
    
        theZeroMQRepServer.Startlisten();
    }
    
    private static void SendMsgToServer()
    {
        ZeroMQREQClient theZeroMQREQClient = new ZeroMQREQClient();
        
        theZeroMQREQClient.SendMsg ("Hello Zvika");
    }
    private static void SetLibraryPath() throws SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
        String libPathProperty = System.getProperty("java.library.path");
        
        System.out.println(libPathProperty);
        
        System.setProperty( "java.library.path", "H:\\deleteme100\\zeromq-4.0.4\\bin\\Win32" );
        
        Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
        fieldSysPath.setAccessible( true );
        fieldSysPath.set( null, null );
    
        libPathProperty = System.getProperty("java.library.path");
        System.out.println(libPathProperty);
    }
}

Note:
The SetLibraryPath method is used to set the path of the jzmq
The jzmq.dll is a jni native extension used for the 0MQ
The Server


package test0mq2;
import org.zeromq.ZMQ;
public class ZeroMQRepServer extends Thread{
    
     public void Startlisten ()
     {
         (new ZeroMQRepServer()).start();
     }
     @Override
     public void run() {
          
        ZMQ.Context context = ZMQ.context(1);
        //  Socket to talk to clients
        ZMQ.Socket responder = context.socket(ZMQ.REP);
        responder.bind("tcp://*:5555");
        while (!Thread.currentThread().isInterrupted()) {
            // Wait for next request from the client
            byte[] request = responder.recv(0);
           
            // Send reply back to client
            String theReplay = "Get:" + new String(request);
            responder.send(theReplay.getBytes(), 0);
        }
        responder.close();
        context.term();
     }
}

 The Client


package test0mq2;
import org.zeromq.ZMQ;
public class ZeroMQREQClient {
    
    public void SendMsg (String pMsg)
    {
        ZMQ.Context context = ZMQ.context(1);
       //Connecting to server with REQ mode 
        ZMQ.Socket requester = context.socket(ZMQ.REQ);
        
        requester.connect("tcp://localhost:5555");
        requester.send(pMsg.getBytes(), 0);
        byte[] reply = requester.recv(0);
        
        System.out.println("Received:" + new String(reply));
        
        requester.close();
        
        context.term();
    }
}

 

 

אין תגובות:

הוסף רשומת תגובה