Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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
- /*
- jAlarms A simple Java library to enable server apps to send alarms to sysadmins.
- Copyright (C) 2009 Enrique Zamudio Lopez
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
- */
- package com.solab.alarms ;
- import java.io.IOException
- import java.net.InetSocketAddress ;
- import java.util.List ;
- import javax.annotation.PostConstruct ;
- import javax.annotation.PreDestroy ;
- import org.slf4j.Logger ;
- import org.slf4j.LoggerFactory ;
- import net.spy.memcached.MemcachedClient ;
- import net.spy.memcached.OperationTimeoutException ;
- public class AlarmMemcachedClient implements AlarmCache {
- private final Logger log = LoggerFactory.getLogger(getClass()) ;
- private MemcachedClient mc ;
- private InetSocketAddress[] servers ;
- private int defint = 120 ;
- public void setDefaultInterval(int value) {
- defint = value ;
- }
- public int getDefaultInterval() {
- return defint ;}
- public void setServers(List<String> value) {
- servers = new InetSocketAddress[value.size()] ;
- int pos = 0 ;
- for (String s : value) {
- int cp = value.indexOf(':') ;
- if (cp > 0) {
- servers[pos++] = new InetSocketAddress(s.substring(0, cp),
- Integer.parseInt(s.substring(cp + 1))) ;}
- else {
- servers[pos++] = new InetSocketAddress(s, 11211) ;
- }}}
- @PostConstruct
- public void init() throws IOException {
- mc = new MemcachedClient(servers) ;
- }
- @PreDestroy
- public void disconnect() {
- mc.shutdown() ;
- }
- @Override
- public void store(AlarmChannel channel, String source, String message) {
- if (mc == null) {
- synchronized (this) {
- if (mc == null) {
- try {
- init() ;}
- catch (IOException ex) {
- log.error("Initializing alarm memcached client", ex) ;
- return ;
- }}}}}
- @Override
- public boolean shouldResend(AlarmChannel channel, String source, String message) {
- if (mc == null) {
- return true ;}
- try {
- return mc.get(k) == null ;}
- catch (OperationTimeoutException ex) {
- log.error("Timeout waiting to retrieve {} from memcached", k, ex) ;}
- catch (RuntimeException ex) {
- log.error("Retrieving key {} from memcached", k, ex.getCause() == null ? ex : ex.getCause()) ;}
- return true ;
- }
- @Override
- public String toString() {
- return String.format("Memcached(%s)", mc == null ? "disconnected" : mc.getAvailableServers()) ;
- }
- @Override
- public void shutdown() {
- if (mc != null) {
- mc.shutdown() ;}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement