View Javadoc
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  
21  import java.util.List;
22  
23  import javax.jms.JMSException;
24  import javax.jms.Message;
25  import javax.jms.MessageListener;
26  import javax.jms.ObjectMessage;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.beans.factory.annotation.Autowired;
31  import org.springframework.stereotype.Service;
32  import org.springframework.transaction.annotation.Transactional;
33  
34  import com.hack23.cia.service.component.agent.impl.worldbank.workers.data.WorldbankUpdateService;
35  import com.hack23.cia.service.external.worldbank.api.DataFailureException;
36  import com.hack23.cia.service.external.worldbank.api.WorldBankDataApi;
37  
38  /**
39   * The Class WorldbankDataWorkConsumerImpl.
40   */
41  @Service("worldbankDataWorkConsumerImpl")
42  @Transactional
43  final class WorldbankDataWorkConsumerImpl implements
44  MessageListener {
45  
46  	/** The Constant LOGGER. */
47  	private static final Logger LOGGER = LoggerFactory
48  			.getLogger(WorldbankDataWorkConsumerImpl.class);
49  
50  	/** The update service. */
51  	@Autowired
52  	private WorldbankUpdateService updateService;
53  
54  	@Autowired
55  	private WorldBankDataApi worldbankDataApi;
56  
57  	/**
58  	 * Instantiates a new worldbank data work consumer impl.
59  	 */
60  	public WorldbankDataWorkConsumerImpl() {
61  		super();
62  	}
63  
64  
65  	@Override
66  	public void onMessage(final Message message) {
67  		try {
68  			final List<String> countryIndicator= (List<String>) ((ObjectMessage) message).getObject();
69  			updateService.updateData(worldbankDataApi.getData(countryIndicator.get(0), countryIndicator.get(1)));
70  		} catch (final DataFailureException | RuntimeException | JMSException e) {
71  			LOGGER.warn("Error loading worldbank data:" , e);
72  		}
73  	}
74  }