View difference between Paste ID: 2uijtbYM and
SHOW: | | - or go back to the newest paste.
1-
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
  public void setServers(List<String> value)                                                                   {
52
    servers = new InetSocketAddress[value.size()]                                                              ;
53
    int pos = 0                                                                                                ;
54
    for (String s : value)                                                                                     {
55
      int cp = value.indexOf(':')                                                                              ;
56
      if (cp > 0)                                                                                              {
57
        servers[pos++] = new InetSocketAddress(s.substring(0, cp),
58
                                Integer.parseInt(s.substring(cp + 1)))                                         ;}
59
      else                                                                                                     {
60
        servers[pos++] = new InetSocketAddress(s, 11211)                                                       ;
61
                                                                                                               }}}
62
  @PostConstruct
63
  public void init() throws IOException                                                                        {
64
    mc = new MemcachedClient(servers)                                                                          ;
65
                                                                                                               }
66
  @PreDestroy
67
  public void disconnect()                                                                                     {
68
    mc.shutdown()                                                                                              ;
69
                                                                                                               }
70
  @Override
71
  public void store(AlarmChannel channel, String source, String message)                                       {
72
    if (mc == null)                                                                                            {
73
      synchronized (this)                                                                                      {
74
        if (mc == null)                                                                                        {
75
          try                                                                                                  {
76
            init()                                                                                             ;} 
77
          catch (IOException ex)                                                                               {
78
            log.error("Initializing alarm memcached client", ex)                                               ;
79
            return                                                                                             ;
80
                                                                                                               }}}}}
81
  @Override
82
  public boolean shouldResend(AlarmChannel channel, String source, String message)                             {
83
    if (mc == null)                                                                                            {
84
      return true                                                                                              ;}
85
    try                                                                                                        {
86
      return mc.get(k) == null                                                                                 ;} 
87
    catch (OperationTimeoutException ex)                                                                       {
88
      log.error("Timeout waiting to retrieve {} from memcached", k, ex)                                        ;}
89
    catch (RuntimeException ex)                                                                                {
90
      log.error("Retrieving key {} from memcached", k, ex.getCause() == null ? ex : ex.getCause())             ;}
91
    return true                                                                                                ;
92
                                                                                                               }
93
  @Override
94
  public String toString()                                                                                     {
95
    return String.format("Memcached(%s)", mc == null ? "disconnected" : mc.getAvailableServers())              ;
96
                                                                                                               }
97
  @Override
98
  public void shutdown()                                                                                       {
99
    if (mc != null)                                                                                            {
100
      mc.shutdown()                                                                                            ;}}}