001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.transport.discovery.http; 018 019import java.io.IOException; 020import java.io.PrintWriter; 021import java.util.ArrayList; 022import java.util.Map; 023import java.util.concurrent.ConcurrentHashMap; 024import java.util.concurrent.ConcurrentMap; 025 026import javax.servlet.ServletException; 027import javax.servlet.http.HttpServlet; 028import javax.servlet.http.HttpServletRequest; 029import javax.servlet.http.HttpServletResponse; 030 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034public class DiscoveryRegistryServlet extends HttpServlet { 035 private static final long serialVersionUID = 1L; 036 037 private static final Logger LOG = LoggerFactory.getLogger(HTTPDiscoveryAgent.class); 038 long maxKeepAge = 1000*60*60; // 1 hour. 039 ConcurrentMap<String, ConcurrentMap<String, Long>> serviceGroups = new ConcurrentHashMap<String, ConcurrentMap<String, Long>>(); 040 041 @Override 042 protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 043 String group = req.getPathInfo(); 044 String service = req.getHeader("service"); 045 LOG.debug("Registering: group="+group+", service="+service); 046 047 ConcurrentMap<String, Long> services = getServiceGroup(group); 048 services.put(service, System.currentTimeMillis()); 049 } 050 051 private ConcurrentMap<String, Long> getServiceGroup(String group) { 052 return serviceGroups.computeIfAbsent(group, k -> new ConcurrentHashMap<String, Long>()); 053 } 054 055 @Override 056 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 057 try { 058 long freshness = 1000*30; 059 String p = req.getParameter("freshness"); 060 if( p!=null ) { 061 freshness = Long.parseLong(p); 062 } 063 064 String group = req.getPathInfo(); 065 LOG.debug("group="+group); 066 ConcurrentMap<String, Long> services = getServiceGroup(group); 067 PrintWriter writer = resp.getWriter(); 068 069 long now = System.currentTimeMillis(); 070 long dropTime = now-maxKeepAge; 071 long minimumTime = now-freshness; 072 073 ArrayList<String> dropList = new ArrayList<String>(); 074 for (Map.Entry<String, Long> entry : services.entrySet()) { 075 if( entry.getValue() > minimumTime ) { 076 writer.println(entry.getKey()); 077 } else if( entry.getValue() < dropTime ) { 078 dropList.add(entry.getKey()); 079 } 080 } 081 082 // We might as well get rid of the really old entries. 083 for (String service : dropList) { 084 services.remove(service); 085 } 086 087 088 } catch (Exception e) { 089 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occured: "+e); 090 } 091 } 092 093 @Override 094 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 095 String group = req.getPathInfo(); 096 String service = req.getHeader("service"); 097 LOG.debug("Unregistering: group="+group+", service="+service); 098 099 ConcurrentMap<String, Long> services = getServiceGroup(group); 100 services.remove(service); 101 } 102 103}