/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.sal.connector.remoterpc.utils; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; import java.util.concurrent.TimeUnit; import junit.framework.Assert; import org.opendaylight.controller.sal.connector.remoterpc.dto.Message; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.zeromq.ZMQ; public class MessagingUtil { private static final Logger _logger = LoggerFactory.getLogger(MessagingUtil.class); public static Runnable startReplyServer(final ZMQ.Context context, final String serverAddress, final int numRequests /*number of requests after which server shuts down*/) { return new Runnable() { @Override public void run() { final ZMQ.Socket socket = context.socket(ZMQ.REP); try { int returnCode = socket.bind("tcp://" + serverAddress); Assert.assertNotSame(-1, returnCode); _logger.info(" Starting reply server[{}] for test...", serverAddress); //for (int i=0;i zmqTermination = new FutureTask(new Runnable() { @Override public void run() { try { if (context != null) context.term(); _logger.debug("ZMQ Context terminated gracefully!"); } catch (Exception e) {/*Ignore and continue shutdown*/} } }, null); exec.execute(zmqTermination); try { zmqTermination.get(1L, TimeUnit.SECONDS); } catch (Exception e) { _logger.debug("ZMQ Context terminated forcefully!"); } exec.shutdownNow(); } }