@Override public void run() { terminate = false; logger.info("Starting echos."); EchoResourceMap echoMap = EchoResourceMap.getEchoMap(); // Get the connection details for the MQ box. String path = amqpURI.getRawPath(); String queue = path.substring(path.indexOf('/', 1) + 1); String virtualHost = uriDecode(amqpURI.getPath().substring(1, path.indexOf('/', 1))); // Prepare a simple message to send to the echo system. This message // will come back in each resource's MQ queue. StreamEcho streamEcho = new StreamEcho(); streamEcho.setHost(virtualHost); streamEcho.setQueue(queue); String guid = UUID.randomUUID().toString(); DateFormat df = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("UTC")); streamEcho.setMessage(guid + ";" + df.format(new Date())); String stringStreamEcho = JsonHelper.ToJson(streamEcho); Thread.currentThread().setName(THREAD_NAME); int sleepInterval = Integer.parseInt(SystemProperties.get("ss.echo_sender_interval")) * 1000; while (true) { try { // Send the message to the Sporting Solution's end-point. HttpServices.getHttpService() .processRequest( "http://api.sportingsolutions.com/rels/stream/batchecho", restItem, stringStreamEcho); logger.info("Batch echo sent: " + stringStreamEcho); // After the message is sent increase the number of echos sent // for all resources. // The number of missed echos is configured in: // conf/sdk.properties using "ss.echo_max_missed_echos" Set<String> defaulters = echoMap.incrAll(Integer.parseInt(SystemProperties.get("ss.echo_max_missed_echos"))); Iterator<String> keyIter = defaulters.iterator(); /* * EchoMap returns a list of resources which appear to have * unresponsive queues (the number of echo retries has been * exceeded. * * At this point we disconnect the queue consumer from the MQ * service. This triggers an action in RabbitMQ which alerts the * resource/fixture of the failure. The resource is then * responsible for communicating the problem to the client code. */ while (keyIter.hasNext()) { String resourceId = keyIter.next(); logger.warn( "Attempting to disconnect resource: " + resourceId + " due maximum number of echo retries reached"); MQListener.disconnect(resourceId); } // The interval between echos is configured in: // conf/sdk.properties using "ss.echo_sender_interval" Thread.sleep(sleepInterval); if (terminate == true) { return; } } catch (Exception ex) { logger.error("An error occured: " + ex); } } }
@Before public void setUp() throws Exception { HttpServices.setHttpServices(mock(HttpServices.class)); EchoSender.terminate(); MQListener.terminate(); WorkQueueMonitor.terminate(); if (svcThreadRunning == false) { ServiceThreadExecutor.createExecutor(); svcThreadRunning = true; } /* * CtagResourceMap.reset(); EchoResourceMap.reset(); * ResourceWorkerMap.reset(); ResourceWorkQueue.reset(); * WorkQueue.reset(); */ ResourceWorkerMap.initWorkerMap(); SystemProperties.setProperty("ss.echo_sender_interval", "20"); SystemProperties.setProperty("ss.echo_max_missed_echos", "3"); connFactory = new ConnectionFactory(); connFactory.setHost("127.0.0.1"); connFactory.setVirtualHost("/fern"); connFactory.setUsername("sportingsolutions@fern"); connFactory.setPassword("sporting"); connection = connFactory.newConnection(); channel = connection.createChannel(); // Prepare test data to create the Resource requestItems = JsonHelper.toRestItems(resources); requestSR = new ServiceRequest(); // requestSR.setAuthToken("AUTH_TOKEN_01"); requestSR.setServiceRestItems(requestItems); restItem = getRestItems(requestSR, "Fern v NotFern"); String responseAMQEndPoint = "[{\"Name\":\"stream\",\"Links\":[{\"Relation\":\"amqp\",\"Href\":\"amqp://sportingsolutions%40fern:[email protected]:5672/fern/" + intTestQueue + "\"}]}]"; responseItems = JsonHelper.toRestItems(responseAMQEndPoint); // responseSR.setAuthToken("AUTH_TOKEN_01"); responseSR.setServiceRestItems(responseItems); // Here we mock http services. It still's try to connect but we have a // response ready for the call doAnswer( new Answer<ServiceRequest>() { public ServiceRequest answer(InvocationOnMock invocation) throws Throwable { return responseSR; } }) .when(HttpServices.getHttpService()) .processRequest( "http://api.sportingsolutions.com/rels/stream/amqp", restItem, "Fern v NotFern"); connectedEventCalled = false; disconnectedEventCalled = false; streamEventCalled = false; }