16 changed files with 338 additions and 19 deletions
@ -0,0 +1,73 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.constructor; |
|||
|
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.id.QueueId; |
|||
import org.thingsboard.server.common.data.queue.ProcessingStrategy; |
|||
import org.thingsboard.server.common.data.queue.Queue; |
|||
import org.thingsboard.server.common.data.queue.SubmitStrategy; |
|||
import org.thingsboard.server.gen.edge.v1.ProcessingStrategyProto; |
|||
import org.thingsboard.server.gen.edge.v1.QueueUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.SubmitStrategyProto; |
|||
import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Component |
|||
@TbCoreComponent |
|||
public class QueueMsgConstructor { |
|||
|
|||
public QueueUpdateMsg constructQueueUpdatedMsg(UpdateMsgType msgType, Queue queue) { |
|||
QueueUpdateMsg.Builder builder = QueueUpdateMsg.newBuilder() |
|||
.setMsgType(msgType) |
|||
.setIdMSB(queue.getId().getId().getMostSignificantBits()) |
|||
.setIdLSB(queue.getId().getId().getLeastSignificantBits()) |
|||
.setName(queue.getName()) |
|||
.setTopic(queue.getTopic()) |
|||
.setPollInterval(queue.getPollInterval()) |
|||
.setPartitions(queue.getPartitions()) |
|||
.setConsumerPerPartition(queue.isConsumerPerPartition()) |
|||
.setPackProcessingTimeout(queue.getPackProcessingTimeout()) |
|||
.setSubmitStrategy(createSubmitStrategyProto(queue.getSubmitStrategy())) |
|||
.setProcessingStrategy(createProcessingStrategyProto(queue.getProcessingStrategy())); |
|||
return builder.build(); |
|||
} |
|||
|
|||
private ProcessingStrategyProto createProcessingStrategyProto(ProcessingStrategy processingStrategy) { |
|||
return ProcessingStrategyProto.newBuilder() |
|||
.setType(processingStrategy.getType().name()) |
|||
.setRetries(processingStrategy.getRetries()) |
|||
.setFailurePercentage(processingStrategy.getFailurePercentage()) |
|||
.setPauseBetweenRetries(processingStrategy.getPauseBetweenRetries()) |
|||
.setMaxPauseBetweenRetries(processingStrategy.getMaxPauseBetweenRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
private SubmitStrategyProto createSubmitStrategyProto(SubmitStrategy submitStrategy) { |
|||
return SubmitStrategyProto.newBuilder() |
|||
.setType(submitStrategy.getType().name()) |
|||
.setBatchSize(submitStrategy.getBatchSize()) |
|||
.build(); |
|||
} |
|||
|
|||
public QueueUpdateMsg constructQueueDeleteMsg(QueueId queueId) { |
|||
return QueueUpdateMsg.newBuilder() |
|||
.setMsgType(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE) |
|||
.setIdMSB(queueId.getId().getMostSignificantBits()) |
|||
.setIdLSB(queueId.getId().getLeastSignificantBits()).build(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.fetch; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.data.queue.Queue; |
|||
import org.thingsboard.server.dao.queue.QueueService; |
|||
|
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class QueuesEdgeEventFetcher extends BasePageableEdgeEventFetcher<Queue> { |
|||
|
|||
private final QueueService queueService; |
|||
|
|||
@Override |
|||
PageData<Queue> fetchPageData(TenantId tenantId, Edge edge, PageLink pageLink) { |
|||
return queueService.findQueuesByTenantId(tenantId, pageLink); |
|||
} |
|||
|
|||
@Override |
|||
EdgeEvent constructEdgeEvent(TenantId tenantId, Edge edge, Queue queue) { |
|||
return EdgeUtils.constructEdgeEvent(tenantId, edge.getId(), EdgeEventType.QUEUE, |
|||
EdgeEventActionType.ADDED, queue.getId(), null); |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.processor; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.id.QueueId; |
|||
import org.thingsboard.server.common.data.queue.Queue; |
|||
import org.thingsboard.server.gen.edge.v1.DownlinkMsg; |
|||
import org.thingsboard.server.gen.edge.v1.QueueUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
@TbCoreComponent |
|||
public class QueueEdgeProcessor extends BaseEdgeProcessor { |
|||
|
|||
public DownlinkMsg processQueueToEdge(EdgeEvent edgeEvent, UpdateMsgType msgType, EdgeEventActionType action) { |
|||
QueueId queueId = new QueueId(edgeEvent.getEntityId()); |
|||
DownlinkMsg downlinkMsg = null; |
|||
switch (action) { |
|||
case ADDED: |
|||
case UPDATED: |
|||
Queue queue = queueService.findQueueById(edgeEvent.getTenantId(), queueId); |
|||
if (queue != null) { |
|||
QueueUpdateMsg queueUpdateMsg = |
|||
queueMsgConstructor.constructQueueUpdatedMsg(msgType, queue); |
|||
downlinkMsg = DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addQueueUpdateMsg(queueUpdateMsg) |
|||
.build(); |
|||
} |
|||
break; |
|||
case DELETED: |
|||
QueueUpdateMsg queueDeleteMsg = |
|||
queueMsgConstructor.constructQueueDeleteMsg(queueId); |
|||
downlinkMsg = DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addQueueUpdateMsg(queueDeleteMsg) |
|||
.build(); |
|||
break; |
|||
} |
|||
return downlinkMsg; |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue