[zeromq-dev] What am I doing wrong?

Michael Scofield bayinamine at gmail.com
Wed Sep 11 04:47:53 CEST 2013


REQ socket is implemented like an FSM, it transforms between "send" and
"recv"(basicly). Thus the IllegalStateException occurred if you tried to
use "send" twice. It's not use to only "invalidate" and "return" the
socket(since you have set the socket's state). You can resent the data
through it only by explicit close the socket like ctx.destroySocket(socket)
and then recreate one.
So the code might look like:
        try {
            req = (Socket) socketPool.borrowObject();

            req.send(baos.toByteArray(), NO_FLAGS); //   private static
final int NO_FLAGS = 0;

            req.setReceiveTimeOut(10000);

            data = req.recv();
        } finally {
            if (req != null) {
                if (data == null) {
                    socketPool.recreateObject(req);
                } else {
                    socketPool.returnObject(req);
                }
            }
        }


2013/9/10 Gonzalo Vasquez <gvasquez at altiuz.cl>

> Folks,
>
> I thought my code was OK, but yesterday I put a Junit Test under ContiPerf
> annotiations to stress the zmq link and booom!!! Several problems arose.
>
> On the client side I have:
>
>  try {
>             req = (Socket) socketPool.borrowObject();
>
>             req.send(baos.toByteArray(), NO_FLAGS); //   private static
> final int NO_FLAGS = 0;
>
>             //req.setReceiveTimeOut(10000);
>
>             data = req.recv();
>             if (data == null) {
>                 socketPool.invalidateObject(req);
>             }
>         } finally {
>             if (req != null) {
>                 socketPool.returnObject(req);
>             }
>         }
>
> The object pool is as follows:
>
> import org.apache.commons.pool.impl.StackObjectPool;
> private static StackObjectPool socketPool = null;
>
>
>      socketPool = new StackObjectPool(new SocketFactory(POOL_SIZE,
>                         url), POOL_SIZE);
>
> And the SocketFactory is:
>
> public class SocketFactory implements PoolableObjectFactory {
>
>     /**
>      * URL de conexión
>      */
>     private static String ENDPOINT = null; //
> Messages.getString("Connector.endpoint");
>
>     /**
>      * Contexto
>      */
>     private Context ctx;
>
>     /**
>      * Constructor.
>      *
>      * @param poolSize
>      *            Tamaño del pool
>      * @param endpoint
>      *            URL de conexión
>      */
>     public SocketFactory(int poolSize, final String endpoint) {
>         ENDPOINT = endpoint;
>         ctx = ZMQ.context(poolSize);
>     }
>
>     /**
>      * {@inheritDoc}
>      */
>     public void activateObject(final Object obj) throws Exception {
>         // En blanco
>         System.out.println("activating");
>     }
>
>     /**
>      * {@inheritDoc}
>      */
>     public void destroyObject(final Object obj) throws Exception {
>         if (obj instanceof Socket) {
>             Socket socket = (Socket) obj;
>             socket.setLinger(0);
>             socket.close();
>             socket.disconnect(ENDPOINT);
>             socket = null;
>         }
>     }
>
>     /**
>      * {@inheritDoc}
>      */
>     public Object makeObject() throws Exception {
>         Socket socket = ctx.socket(ZMQ.REQ);
>         socket.connect(ENDPOINT);
>         return socket;
>     }
>
>     /**
>      * {@inheritDoc}
>      */
>     public void passivateObject(final Object obj) throws Exception {
>         // En blanco
>         System.out.println("pss");
>     }
>
>     /**
>      * {@inheritDoc}
>      */
>     public boolean validateObject(final Object obj) {
>         Socket socket=(Socket) obj;
>         return socket != null;
>     }
>
> }
>
>
> Initially the code was getting hang / blocking a the req.recv() line, so I
> added the receive timeout to force errors when the server has problems, but
> then I came across the following message:
>
> java.lang.IllegalStateException: Cannot send another request
> at zmq.Req.xsend(Req.java:51)
> at zmq.SocketBase.send(SocketBase.java:597)
> at org.zeromq.ZMQ$Socket.send(ZMQ.java:982)
>
> I'm aware that this arises due to the fact that the socket is still in
> "recv" state, though it cannot send data, but upon object invalidation
> (when data==null) the sockets are closed, thus supossed to be reseted, but
> that doesn't seem to be working.
>
> FYI: I'm using Java (quite obvious from above), but with JeroMQ SNAPSHOT
> 0.3.0
>
> Any ideas on why the socket isn't being closed/reset? What am I doing
> wrong?
>
> Regards,
>
>   Gonzalo Vásquez Sáez
> Gerente Investigación y Desarrollo (R&D)
> Altiuz Soluciones Tecnológicas de Negocios Ltda.
> Av. Nueva Tajamar 555 Of. 802, Las Condes - CP 7550099
> +56 2 335 2461
>   gvasquez at altiuz.cl
> http://www.altiuz.cl
> http://www.altiuzreports.com
>   <https://www.facebook.com/altiuz>  <http://twitter.com/altiuz> <http://www.linkedin.com/company/altiuz>
>
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130911/4f76f1cb/attachment.htm>


More information about the zeromq-dev mailing list