Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * Service Activity Monitoring :: Agent
   * %%
   * Copyright (C) 2011 Talend Inc.
   * %%
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * 
  *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * #L%
  */
 package org.talend.esb.sam.agent.collector;
 
 import java.util.List;
 
Event collector collects all events and stores them in a queue. This can be a memory queue or a persistent queue. Asynchronously the events will be processed and sent to MonitoringService
 
 
     private static Logger logger = Logger.getLogger(EventCollectorImpl.class.getName());
 
     private Bus bus;
     @Autowired(required = false)
     private List<EventFilterfilters = new ArrayList<EventFilter>();
 
     private List<EventHandlerhandlers = new ArrayList<EventHandler>();
     private Queue<Eventqueue = new ConcurrentLinkedQueue<Event>();
     private TaskExecutor executor;
     private TaskScheduler scheduler;
     private long defaultInterval = 1000;
     private int eventsPerMessageCall = 10;
     private boolean stopSending = false;
 
     public EventCollectorImpl() {
     }

    
Returns the number of events sent by one service call.

Returns:
 
     public int getEventsPerMessageCall() {
         if ( <= 0) {
             .warning("Message package size is not set or is lower then 1. Set package size to 1.");
             return 1;
         }
         return ;
     }

    
Set by Spring. Define how many events will be sent within one service call.

Parameters:
eventsPerMessageCall
 
     public void setEventsPerMessageCall(int eventsPerMessageCall) {
         this. = eventsPerMessageCall;
     }

    
Returns the default interval for sending events

Returns:
 
     private long getDefaultInterval() {
         return ;
     }

    
Set default interval for sending events to monitoring service. DefaultInterval will be used by scheduler.

Parameters:
defaultInterval
    public void setDefaultInterval(long defaultInterval) {
        this. = defaultInterval;
    }

    
Scheduler will be set and configured by Spring. Spring executes every x milliseconds the sending process.

Parameters:
scheduler
    public void setScheduler(TaskScheduler scheduler) {
        .info("Scheduler startet for sending events to monitoring service");
        this. = scheduler;
        this..scheduleAtFixedRate(new Runnable() {
            public void run() {
                sendEventsFromQueue();
            }
        }, getDefaultInterval());
    }

    
Spring sets the executor. The executer is used for sending events to the web service.

Parameters:
executor
    public void setExecutor(TaskExecutor executor) {
        this. = executor;
    }

    
Spring sets the queue. Within the spring configuration you can decide between memory queue and persistent queue.

Parameters:
queue
    public void setQueue(Queue<Eventqueue) {
        this. = queue;
    }

    
Spring sets the monitoring service client.

Parameters:
monitoringServiceClient
    public void setMonitoringServiceClient(MonitoringService monitoringServiceClient) {
        this. = monitoringServiceClient;
    }
    public void setBus(Bus bus) {
        this. = bus;
    }
    public List<EventFiltergetFilters() {
        return ;
    }
    public void setFilters(List<EventFilterfilters) {
        this. = filters;
    }
    public List<EventHandlergetHandlers() {
        return ;
    }
    @Autowired(required = false)
    public void setHandlers(List<EventHandlernewHandlers) {
        this..clear();
        for (EventHandler eventHandler : newHandlers) {
            // TODO This shows that autowiring like we do it now is not such a good idea
            if (!(eventHandler instanceof QueueSender)) {
                this..add(eventHandler);
            }
        }
    }

    
Method will be executed asynchronously from spring.
    public void sendEventsFromQueue() {
        if (this. == true) {
            return;
        }
        .fine("Scheduler called for sending events");
        int packageSize = getEventsPerMessageCall();
        while (!.isEmpty()) {
            final List<Eventlist = new ArrayList<Event>();
            int i = 0;
            while (i < packageSize && !.isEmpty()) {
                Event event = .remove();
                if (event != null && !filter(event)) {
                    list.add(event);
                    i++;
                }
            }
            if (list.size() > 0) {
                // Need to call executer because @Async wouldn't work
                // through proxy. This Method is inside a proxy and local
                // proxy calls are not supported
                .execute(new Runnable() {
                    public void run() {
                        try {
                            sendEvents(list);
                        } catch (MonitoringException e) {
                            e.logException(.);
                        }
                    }
                });
            }
        }
    }

    
Execute all filters for the event.

Parameters:
event
Returns:
    private boolean filter(Event event) {
        for (EventFilter filter : ) {
            if (filter.filter(event) == true)
                return true;
        }
        return false;
    }

    
Sends the events to monitoring service client.

Parameters:
events
    private void sendEvents(final List<Eventevents) {
        for (EventHandler current : ) {
            for (Event event : events) {
                current.handleEvent(event);
            }
        }
        .info("Put events(" + events.size() + ") to Monitoring Server.");
        try {
            .putEvents(events);
        } catch (Exception e) {
            if (e instanceof MonitoringException)
                throw (MonitoringException)e;
            throw new MonitoringException("002",
                                          "Unknown error while execute put events to Monitoring Server"e);
        }
    }
    public void stopSending() {
    }
    @Override
    public void initComplete() {
        // Ignore
    }
    @Override
    public void preShutdown() {
        .info("Bus is stopping. Stopping sending events to monitoring service.");
        this. = true;
    }
    @Override
    public void postShutdown() {
        // Ignore
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        if ( != null) {
            BusLifeCycleManager lm = .getExtension(BusLifeCycleManager.class);
            if (null != lm) {
                lm.registerLifeCycleListener(this);
            }
        }
    }
New to GrepCode? Check out our FAQ X