我正在做一个使用spring-solr从核心获取数据,处理数据然后将其保存到同一核心的cron作业。 与solr的连接和cron作业的执行不是问题, (我的意思是我使用简单的消息'Hello world'测试了cron作业,并使用独立的REST服务测试了solr连接)。 我的问题是这行执行时得到一个空指针异常错误:
List<Usuario> turistas = viajeroRepository.findAllCities();
这是该类的完整代码:
@Service
public class NormalizeCities extends QuartzJobBean {
private static final Logger logger = Logger.getLogger(NormalizeCities.class);
@Autowired
private ViajeroRepository viajeroRepository;
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
List<Usuario> turistas = viajeroRepository.findAllCities();
Set<String> cities = new HashSet<String>();
for (Usuario turista : turistas)
cities.add(turista.getReviewer_city());
int counter = 0;
Iterator<String> iterator = cities.iterator();
while(iterator.hasNext()) {
String city = iterator.next();
JSONObject json = new JSONObject(Util.readUrl(Util.getGoogleURL(Util.normalize(city), counter)));
String status = json.getString("status");
if (status.equals("OK")) {
processJson(json,city);
} else if (status.equals("OVER_QUERY_LIMIT")) {
counter++;
if (counter == 4) {
counter = 0;
}
json = new JSONObject(Util.readUrl(Util.getGoogleURL(
Util.normalize(city), counter)));
processJson(json,city);
}
}
logger.info("HECHO:Normalizacion de ciudades");
}
private void processJson(JSONObject json,String city) {
json = json.getJSONArray("results").getJSONObject(0);
String cityName = json.getString("formatted_address");
json = json.getJSONObject("geometry").getJSONObject("location");
Double lat = json.getDouble("lat");
Double lng = json.getDouble("lng");
List<Usuario> viajeros = viajeroRepository.findAllCitiesByLocation(city);
for(int index=0;index<viajeros.size();index++){
viajeros.get(index).setReviewer_city(cityName);
viajeros.get(index).setLat(lat);
viajeros.get(index).setLng(lng);
}
viajeroRepository.save(viajeros);
}
}
solr存储库代码:
public interface ViajeroRepository extends SolrCrudRepository<Usuario, String> {
@Query(value="reviewer_city:* AND -lat:* AND -lng:*", fields = {"reviewer_city"})
List<Usuario> findAllCities();
@Query(value="reviewer_city:?0 AND -lat:* AND -lng:*")
List<Usuario> findAllCitiesByLocation(String reviewer_city);
@Query(value="id_destino:?0 AND reviewer_city:* AND lat:* AND lng:*", fields = {"reviewer_city","lat","lng"})
List<Usuario> findOriginCityCoordinates(String id_destino);
}
quartz.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="job" class="mx.sectur.job.NormalizeCities" />
<!-- Quartz Job -->
<bean name="Job" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="mx.sectur.job.NormalizeCities" />
</bean>
<!-- Cron Trigger -->
<bean id="cronTriggerJob"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="Job" />
<property name="cronExpression" value="0 05 14 ? * *" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTriggerJob" />
</list>
</property>
</bean>
</beans>
stacktrace:
2017-05-26 14:05:00 ERROR JobRunShell:211 - Job DEFAULT.Job threw an unhandled E
xception:
java.lang.NullPointerException
at mx.sectur.job.NormalizeCities.executeInternal(NormalizeCities.java:32
)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJob
Bean.java:75)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.j
ava:573)
2017-05-26 14:05:00 ERROR ErrorLogger:2425 - Job (DEFAULT.Job threw an exception
.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exc
eption: java.lang.NullPointerException]
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.j
ava:573)
Caused by: java.lang.NullPointerException
at mx.sectur.job.NormalizeCities.executeInternal(NormalizeCities.java:32
)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJob
Bean.java:75)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
我是否错过了在cron工作中配置的内容?
答案 0 :(得分:0)
您应该知道
的确切内容viajeroRepository
但是看看你的代码,我认为依赖注入不适用于{{1}}。