001 package biz.hammurapi.metrics; 002 003 import java.io.ObjectOutputStream; 004 import java.net.Socket; 005 import java.util.List; 006 007 import biz.hammurapi.config.ConfigurationException; 008 009 /** 010 * Sends slices to remote slice consumer server. 011 * @author Pavel Vlasov 012 */ 013 public class SocketSliceConsumer extends BatchingSliceConsumer { 014 015 public static final int DEFAULT_PORT = 9814; 016 private static final String LOCALHOST = "localhost"; 017 018 private String address = LOCALHOST; 019 private int port = DEFAULT_PORT; 020 private String id = Long.toString(System.currentTimeMillis(), Character.MAX_RADIX); 021 022 /** 023 * Sets server address. Default is "localhost" 024 * @param address 025 */ 026 public void setAddress(String address) { 027 this.address = address; 028 } 029 030 /** 031 * Sets server port. Default is 9814 032 * @param port 033 */ 034 public void setPort(int port) { 035 this.port = port; 036 } 037 038 /** 039 * Sets client ID to differentiate it from other clients. 040 * Defaults to 041 * @param id 042 */ 043 public void setId(String id) { 044 this.id = id; 045 } 046 047 public void start() throws ConfigurationException { 048 super.start(); 049 System.out.println("[SocketSliceConsumer] ID="+id); 050 } 051 052 /** 053 * Sends slices to a server socket. 054 */ 055 protected boolean processSlices(List slices) { 056 try { 057 Socket socket = new Socket(address, port); 058 ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); 059 oos.writeObject(id); 060 oos.writeObject(slices); 061 oos.close(); 062 socket.close(); 063 return true; 064 } catch (Exception e) { 065 System.err.println("[Socket slice consumer] ERROR: Could not send slices to the server: "+e); 066 return false; 067 } 068 } 069 070 }