001package com.dpillay.tools.tail4j.core; 002 003import java.util.concurrent.BlockingQueue; 004import java.util.concurrent.LinkedBlockingQueue; 005 006import com.dpillay.tools.tail4j.exception.ApplicationException; 007import com.dpillay.tools.tail4j.exception.ErrorCode; 008import com.dpillay.tools.tail4j.model.TailContents; 009import com.dpillay.tools.tail4j.model.TailEvent; 010 011public class TailListener<T> { 012 private BlockingQueue<TailEvent<T>> tailEventQueue = new LinkedBlockingQueue<TailEvent<T>>(); 013 014 public void onTail(TailEvent<T> te) { 015 this.tailEventQueue.offer(te); 016 } 017 018 public TailContents<T> poll() throws ApplicationException { 019 try { 020 TailEvent<T> te = null; 021 te = this.tailEventQueue.take(); 022 if (te != null) 023 return te.getTailContents(); 024 } catch (InterruptedException e) { 025 throw new ApplicationException(e, ErrorCode.IO_ERROR, 026 "Could not take tail event from the queue"); 027 } 028 throw new ApplicationException(ErrorCode.POLL_ERROR, 029 "Could not take tail event from the queue"); 030 } 031}