Advertisement
Guest User

Untitled

a guest
Dec 27th, 2010
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.98 KB | None | 0 0
  1. //http://jalarms.svn.sourceforge.net/viewvc/jalarms/maven-project/src/main/java/com/solab/alarms/AlarmMemcachedClient.java?revision=59&content-type=text/plain&pathrev=59
  2.  
  3. /*
  4. jAlarms A simple Java library to enable server apps to send alarms to sysadmins.
  5. Copyright (C) 2009 Enrique Zamudio Lopez
  6.  
  7. This library is free software you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation either
  10. version 2.1 of the License, or (at your option) any later version.
  11.  
  12. This library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. Lesser General Public License for more details.
  16.  
  17. You should have received a copy of the GNU Lesser General Public
  18. License along with this library if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  20. */
  21.  
  22. package com.solab.alarms                                                                                      
  23.  
  24. import java.io.IOException                                                                              
  25. import java.net.InetSocketAddress                                                                              
  26. import java.util.List                                                                                          
  27.  
  28. import javax.annotation.PostConstruct                                                                          
  29. import javax.annotation.PreDestroy                                                                            
  30.  
  31. import org.slf4j.Logger                                                                                        
  32. import org.slf4j.LoggerFactory                                                                                
  33.  
  34. import net.spy.memcached.MemcachedClient                                                                      
  35. import net.spy.memcached.OperationTimeoutException                                                            
  36.  
  37.  
  38. public class AlarmMemcachedClient implements AlarmCache                                                        
  39.  
  40.   private final Logger log = LoggerFactory.getLogger(getClass())                                              
  41.   private MemcachedClient mc                                                                                  
  42.   private InetSocketAddress[] servers                                                                          
  43.   private int defint = 120                                                                                    
  44.  
  45.   public void setDefaultInterval(int value)                                                                    
  46.     defint = value                                                                                            
  47.                                                                                                                
  48.   public int getDefaultInterval()                                                                              
  49.     return defint                                                                                              
  50.                                                                                                                
  51.  
  52.   public void setServers(List<String> value)                                                                  
  53.     servers = new InetSocketAddress[value.size()]                                                              
  54.     int pos = 0                                                                                                
  55.     for (String s : value)                                                                                    
  56.       int cp = value.indexOf(':')                                                                              
  57.       if (cp > 0)                                                                                              
  58.         servers[pos++] = new InetSocketAddress(s.substring(0, cp),
  59.                                 Integer.parseInt(s.substring(cp + 1)))                                        
  60.       else                                                                                                    
  61.         servers[pos++] = new InetSocketAddress(s, 11211)                                                      
  62.                                                                                                                
  63.                                                                                                                
  64.                                                                                                                
  65.  
  66.   @PostConstruct
  67.   public void init() throws IOException                                                                        
  68.     mc = new MemcachedClient(servers)                                                                          
  69.                                                                                                                
  70.  
  71.   @PreDestroy
  72.   public void disconnect()                                                                                    
  73.     mc.shutdown()                                                                                              
  74.                                                                                                                
  75.  
  76.   @Override
  77.   public void store(AlarmChannel channel, String source, String message)                                      
  78.     if (mc == null)                                                                                            
  79.       synchronized (this)                                                                                      
  80.         if (mc == null)                                                                                        
  81.           try                                                                                                  
  82.             init()                                                                                              
  83.           catch (IOException ex)                                                                              
  84.             log.error("Initializing alarm memcached client", ex)                                              
  85.             return                                                                                            
  86.                                                                                                                
  87.                                                                                                                
  88.                                                                                                                
  89.                                                                                                                
  90.                                                                                                                
  91.  
  92.   @Override
  93.   public boolean shouldResend(AlarmChannel channel, String source,
  94.       String message)                                                                                          
  95.     if (mc == null)                                                                                            
  96.       return true                                                                                              
  97.                                                                                                                
  98.     try                                                                                                        
  99.       return mc.get(k) == null                                                                                  
  100.     catch (OperationTimeoutException ex)                                                                      
  101.       log.error("Timeout waiting to retrieve  from memcached", k, ex)                                        
  102.     catch (RuntimeException ex)                                                                                
  103.       log.error("Retrieving key  from memcached", k, ex.getCause() == null ? ex : ex.getCause())            
  104.     return true                                                                                                
  105.                                                                                                                
  106.  
  107.   @Override
  108.   public String toString()                                                                                    
  109.     return String.format("Memcached(%s)", mc == null ? "disconnected" : mc.getAvailableServers())              
  110.                                                                                                                
  111.  
  112.   @Override
  113.   public void shutdown()                                                                                      
  114.     if (mc != null)                                                                                            
  115.       mc.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement