我有两个课
@Component
public class EventProcessor {
private static final Logger LOGGER = LoggerFactory.getLogger(EventProcessor.class);
@Autowired
private MyProperties myProperties;
/**
* Listens to the Event hub
*
* @throws InterruptedException
* @throws ExecutionException
*/
public void startProcess() throws Exception {
EventProcessorHost host = new EventProcessorHost(
EventProcessorHost.createHostName(myProperties.getEventproperties().getHostNamePrefix(),
myProperties.getEventproperties().getEventHubName(),
myProperties.getEventproperties().getConsumerGroupName(),
myProperties.getEventproperties().getEventHubConnectionString(),
myProperties.getEventproperties().getStorageConnectionString(),
myProperties.getEventproperties().getStorageContainerName());
EventProcessorOptions options = new EventProcessorOptions();
host.registerEventProcessor(AzureEventHubReceiver.class, options).get();
}
}
和
@Component
public class AzureEventHubReceiver implements IAzureEventHubReceiver {
private static final Logger LOGGER = LoggerFactory.getLogger(AzureEventHubReceiver.class);
private int checkpointBatchingCount = 0;
@Autowired
private IDeployStatusService deployStatusService;
@Override
public void onOpen(PartitionContext context) throws Exception {
}
@Override
public void onClose(PartitionContext context, CloseReason reason) throws Exception {
}
@Override
public void onError(PartitionContext context, Throwable error) {
}
/*
*
*/
@Override
public void onEvents(PartitionContext context, Iterable<EventData> events)
throws InterruptedException, ExecutionException, JsonParseException, JsonMappingException, IOException {
for (EventData data : events) {
// Gson gson = new GsonBuilder().create();
ObjectMapper objectMapper = new ObjectMapper();
// EventHubModel model = null;
EventHubModel[] model = objectMapper.readValue(new String(data.getBytes(), "UTF8"), EventHubModel[].class);
if(model!=null && deployStatusService!=null) {
deployStatusService.saveOrUpdateToDb(model[0].getData().getCorrelationId(), model[0].getData().getStatus());
}
this.checkpointBatchingCount++;
if ((checkpointBatchingCount % 5) == 0) {
context.checkpoint(data).get();
}
}
}}
使用commandLineRunner在springboot启动时加载事件处理器类
@Component
public class EventProcessorRunner implements CommandLineRunner{
@Autowired
private EventProcessor processor;
EventProcessorRunner(final EventProcessor processor) {
this.processor = processor;
}
@Override
public void run(String... args) throws Exception {
processor.startProcess();
}
}
IDeployStatus接口如下
public interface IDeployStatusService {
void saveCorrelationId(String correlationId,Long serviceId, Long userSubscriptionId, ProvisioningState provisioningState);
void saveOrUpdateToDb(String correlationId, String status);
}
实现是
@Service
public class DeployStatusService implements IDeployStatusService {
private static final Logger LOGGER = LoggerFactory.getLogger(DeployStatusService.class);
@Autowired
IServiceConverter serviceConverter;
@Autowired
ServiceRepo serviceRepo;
@Autowired
IDeployConverter deployConverter;
@Autowired
ISubConverter subConverter;
@Autowired
SubRepository subRepo;
@Autowired
ServiceDeployRepository serviceDeployRepository;
@Override
public void saveCorrelationId(String correlationId, Long serviceId, Long userSubscriptionId,
ProvisioningState provisioningState) {
}
但是,即使从API触发了一个事件并且控件到达此处,IDeployStatusService仍未初始化,并且始终返回null。 我曾尝试在deployStatusService上进行构造函数注入,但无济于事。