001/*
002 * #%L
003 * Netarchivesuite - archive
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.archive.bitarchive.distribute;
024
025import java.util.TimerTask;
026
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030import dk.netarkivet.common.distribute.ChannelID;
031import dk.netarkivet.common.distribute.JMSConnection;
032import dk.netarkivet.common.distribute.JMSConnectionFactory;
033import dk.netarkivet.common.exceptions.ArgumentNotValid;
034import dk.netarkivet.common.exceptions.IOFailure;
035
036/**
037 * Thread reponsible for sending out periodic HeartBeatMessages. The BitarchiveServer is closed down if any error occurs
038 * whilst sending heartbeats.
039 */
040public class HeartBeatSender extends TimerTask {
041    private static final Logger log = LoggerFactory.getLogger(HeartBeatSender.class);
042    /** the receiver to receive heartbeats. */
043    private ChannelID receiver;
044    /** the BitarchiveServer of this HeartBeatSender. */
045    private BitarchiveServer baServer;
046    /** the id of the application sending the heartbeat message. */
047    private String applicationId;
048    /** the connection to use when sending heartbeats. */
049    private JMSConnection con;
050
051    /**
052     * Constructs a HearBeatSender that sends heartbeats.
053     *
054     * @param inReceiver - the receiver to receive the heartbeats
055     * @param inBaServer - the BitarchiveServer of this HeartBeatSender
056     * @throws ArgumentNotValid - if in_baServer is null
057     * @throws IOFailure - if getting an JMSConnection instance fails
058     */
059    public HeartBeatSender(ChannelID inReceiver, BitarchiveServer inBaServer) throws ArgumentNotValid, IOFailure {
060        ArgumentNotValid.checkNotNull(inBaServer, "inBaServer");
061        receiver = inReceiver;
062        applicationId = inBaServer.getBitarchiveAppId();
063        baServer = inBaServer;
064        con = JMSConnectionFactory.getInstance();
065    }
066
067    /**
068     * This is the run method of the thread sending heartbeats. The BitarchiveServer is closed down if any error occurs.
069     */
070    public void run() {
071        try {
072            con.send(new HeartBeatMessage(receiver, applicationId));
073        } catch (Throwable t) {
074            log.error("An unexpected error occurred. BitarchiveServer couldn't ping BitarchiveMonitorServer.", t);
075        }
076    }
077
078    /**
079     * Retrieval of a string representation of this instance.
080     *
081     * @return The string representation of this instance.
082     */
083    public String toString() {
084        return super.toString() + ", Receiver: " + receiver + ", BitArchiveServer: " + baServer.toString();
085    }
086
087}