WorldbankDataWorkConsumerImpl.java

  1. /*
  2.  * Copyright 2010 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.service.component.agent.impl.worldbank.workers;

  20. import java.util.List;

  21. import javax.jms.JMSException;
  22. import javax.jms.Message;
  23. import javax.jms.MessageListener;
  24. import javax.jms.ObjectMessage;

  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.transaction.annotation.Transactional;

  30. import com.hack23.cia.service.component.agent.impl.worldbank.workers.data.WorldbankUpdateService;
  31. import com.hack23.cia.service.external.worldbank.api.DataFailureException;
  32. import com.hack23.cia.service.external.worldbank.api.WorldBankDataApi;

  33. /**
  34.  * The Class WorldbankDataWorkConsumerImpl.
  35.  */
  36. @Service("worldbankDataWorkConsumerImpl")
  37. @Transactional
  38. final class WorldbankDataWorkConsumerImpl implements
  39. MessageListener {

  40.     /** The Constant LOGGER. */
  41.     private static final Logger LOGGER = LoggerFactory
  42.             .getLogger(WorldbankDataWorkConsumerImpl.class);

  43.     /** The update service. */
  44.     @Autowired
  45.     private WorldbankUpdateService updateService;

  46.     @Autowired
  47.     private WorldBankDataApi worldbankDataApi;

  48.     /**
  49.      * Instantiates a new worldbank data work consumer impl.
  50.      */
  51.     public WorldbankDataWorkConsumerImpl() {
  52.         super();
  53.     }


  54.     @Override
  55.     public void onMessage(final Message message) {
  56.         try {
  57.             final List<String> countryIndicator= (List<String>) ((ObjectMessage) message).getObject();
  58.             updateService.updateData(worldbankDataApi.getData(countryIndicator.get(0), countryIndicator.get(1)));
  59.         } catch (final DataFailureException | RuntimeException | JMSException e) {
  60.             LOGGER.warn("Error loading worldbank data:" , e);
  61.         }
  62.     }
  63. }