/** * This method is here to try and isolate the Azure related code as the PartitionReceiver cannot be mocked * with PowerMock due to it being final. Unfortunately it extends a base class and does not implement an interface * so even if we create a MockPartitionReciver, it will not work as the two classes are orthogonal. * * @param context - The processcontext for this processor * @param partitionId - The partition ID to retrieve a receiver by. * @return - Returns the events received from the EventBus. * @throws ProcessException -- If any exception is encountered, receiving events it is wrapped in a ProcessException * and then that exception is thrown. */ protected Iterable<EventData> receiveEvents(final ProcessContext context, final String partitionId) throws ProcessException { final PartitionReceiver receiver; try { receiver = getReceiver(context, partitionId); return receiver.receive(receiverFetchSize).get(); } catch (final IOException | ServiceBusException | ExecutionException | InterruptedException e) { throw new ProcessException(e); } }
/** * Synchronous version of {@link #receive}. * * @param maxEventCount maximum number of {@link EventData}'s that this call should return * @return Batch of {@link EventData}'s from the partition on which this receiver is created. Returns 'null' if no {@link EventData} is present. * @throws EventHubException if ServiceBus client encountered any unrecoverable/non-transient problems during {@link #receive} */ default Iterable<EventData> receiveSync(final int maxEventCount) throws EventHubException { return ExceptionUtil.sync(() -> this.receive(maxEventCount).get()); }
@Override public void accept(PartitionReceiver receiver) { while (true) { try { Iterable<EventData> receivedEvents = receiver.receive(MAX_MESSAGES).get(); int batchSize = 0; if (receivedEvents != null) { for (EventData receivedEvent : receivedEvents) { String deviceId = (String) receivedEvent.getProperties().get("iothub-connection-device-id"); Map<String, Object> deviceState = null; try { deviceState = ParaObjectUtils.getJsonReader(Map.class).readValue(receivedEvent.getBytes()); logger.debug("Message received from Azure: {}", deviceState); } catch (Exception e) { } if (deviceState != null) { Thing t = thingFromCloudID(deviceId); if (t != null) { t.setDeviceState(deviceState); Para.getDAO().update(t.getAppid(), t); } } batchSize++; } } logger.debug("Received {} messages from Azure for partition {}.", batchSize, partitionId); } catch (Exception e) { logger.warn("Failed to receive messages: {}", e.getMessage()); } } }
@Override public void accept(PartitionReceiver receiver) { while (true) { try { Iterable<EventData> receivedEvents = receiver.receive(MAX_MESSAGES).get(); int batchSize = 0; if (receivedEvents != null) { for (EventData receivedEvent : receivedEvents) { String deviceId = (String) receivedEvent.getProperties().get("iothub-connection-device-id"); Map<String, Object> deviceState = null; try { deviceState = ParaObjectUtils.getJsonReader(Map.class).readValue(receivedEvent.getBytes()); logger.debug("Message received from Azure: {}", deviceState); } catch (Exception e) { } if (deviceState != null) { Thing t = thingFromCloudID(deviceId); if (t != null) { t.setDeviceState(deviceState); Para.getDAO().update(t.getAppid(), t); } } batchSize++; } } logger.debug("Received {} messages from Azure for partition {}.", batchSize, partitionId); } catch (Exception e) { logger.warn("Failed to receive messages: {}", e.getMessage()); } } }