|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 3 | + * the License. You may obtain a copy of the License at |
| 4 | + * <p> |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * <p> |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 8 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 9 | + * specific language governing permissions and limitations under the License. |
| 10 | + */ |
| 11 | + |
| 12 | +package org.apache.eventmesh.runtime.core.protocol.amqp.consumer; |
| 13 | + |
| 14 | + |
| 15 | +import org.apache.eventmesh.common.protocol.amqp.AmqpMessage; |
| 16 | +import org.apache.eventmesh.runtime.core.protocol.amqp.processor.AmqpConnection; |
| 17 | +import org.apache.eventmesh.runtime.core.protocol.amqp.remoting.AMQData; |
| 18 | +import org.apache.eventmesh.runtime.core.protocol.amqp.remoting.AMQPFrame; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import io.netty.buffer.ByteBuf; |
| 25 | + |
| 26 | +import com.rabbitmq.client.AMQP; |
| 27 | +import com.rabbitmq.client.impl.Frame; |
| 28 | +import com.rabbitmq.client.impl.Method; |
| 29 | + |
| 30 | +import lombok.extern.log4j.Log4j2; |
| 31 | + |
| 32 | +/** |
| 33 | + * Used to process command output. |
| 34 | + */ |
| 35 | +@Log4j2 |
| 36 | +public class AmqpMessageSender { |
| 37 | + |
| 38 | + private final AmqpConnection connection; |
| 39 | + |
| 40 | + // |
| 41 | + public AmqpMessageSender(AmqpConnection connection) { |
| 42 | + this.connection = connection; |
| 43 | + } |
| 44 | + |
| 45 | + public long writeDeliver(final AmqpMessage message, int channelId, |
| 46 | + boolean isRedelivered, long deliveryTag, |
| 47 | + String consumerTag) throws IOException { |
| 48 | + AMQP.Basic.Deliver deliver = connection.getCommandFactory().createBasicDeliverBody(consumerTag, deliveryTag, isRedelivered, |
| 49 | + message.getExchange(), message.getRoutingKey()); |
| 50 | + return writeMessage(message, (Method) deliver, channelId); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + public long writeGetOk(final AmqpMessage message, int channelId, |
| 55 | + boolean isRedelivered, long deliveryTag, int messageCount) throws IOException { |
| 56 | + |
| 57 | + AMQP.Basic.GetOk getOk = connection.getCommandFactory().createBasicGetOkBody(deliveryTag, isRedelivered, |
| 58 | + message.getExchange(), message.getRoutingKey(), messageCount); |
| 59 | + return writeMessage(message, (Method) getOk, channelId); |
| 60 | + } |
| 61 | + |
| 62 | + private long writeMessage(final AmqpMessage message, final Method method, int channelId) throws IOException { |
| 63 | + CompositeMessageBlock messageFrame = new CompositeMessageBlock(); |
| 64 | + byte[] body = message.getContentBody(); |
| 65 | + int bodyLength = body.length; |
| 66 | + Frame headerFrame = message.getContentHeader().toFrame(channelId, bodyLength); |
| 67 | + |
| 68 | + int frameMax = connection.getMaxFrameSize(); |
| 69 | + boolean cappedFrameMax = frameMax > 0; |
| 70 | + int bodyPayloadMax = cappedFrameMax ? frameMax - AMQPFrame.NON_BODY_SIZE : bodyLength; |
| 71 | + |
| 72 | + if (cappedFrameMax && headerFrame.size() > frameMax) { |
| 73 | + String msg = String.format("Content headers exceeded max frame size: %d > %d", headerFrame.size(), frameMax); |
| 74 | + throw new IllegalArgumentException(msg); |
| 75 | + } |
| 76 | + messageFrame.setMethod(AMQPFrame.get(method.toFrame(channelId))); |
| 77 | + messageFrame.setHeader(AMQPFrame.get(headerFrame)); |
| 78 | + |
| 79 | + for (int offset = 0; offset < body.length; offset += bodyPayloadMax) { |
| 80 | + int remaining = body.length - offset; |
| 81 | + |
| 82 | + int fragmentLength = (remaining < bodyPayloadMax) ? remaining |
| 83 | + : bodyPayloadMax; |
| 84 | + Frame frame = Frame.fromBodyFragment(channelId, body, |
| 85 | + offset, fragmentLength); |
| 86 | + messageFrame.addContent(AMQPFrame.get(frame)); |
| 87 | + } |
| 88 | + connection.writeFrame(messageFrame); |
| 89 | + return body.length; |
| 90 | + } |
| 91 | + |
| 92 | + public void writeReturn(final AmqpMessage message, int channelId, int replyCode, |
| 93 | + String replyText) throws IOException { |
| 94 | + |
| 95 | + AMQP.Basic.Return returnBody = connection.getCommandFactory().createBasicReturnBody(replyCode, replyText, message.getExchange(), message.getRoutingKey()); |
| 96 | + |
| 97 | + writeMessage(message, (Method) returnBody, channelId); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + public class CompositeMessageBlock implements AMQData { |
| 102 | + |
| 103 | + AMQPFrame method; |
| 104 | + |
| 105 | + AMQPFrame header; |
| 106 | + |
| 107 | + List<AMQPFrame> contents; |
| 108 | + |
| 109 | + @Override |
| 110 | + public void encode(ByteBuf buf) { |
| 111 | + method.encode(buf); |
| 112 | + header.encode(buf); |
| 113 | + contents.forEach(content -> { |
| 114 | + content.encode(buf); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + public CompositeMessageBlock() { |
| 119 | + } |
| 120 | + |
| 121 | + public CompositeMessageBlock(AMQPFrame method, AMQPFrame header, |
| 122 | + List<AMQPFrame> contents) { |
| 123 | + this.method = method; |
| 124 | + this.header = header; |
| 125 | + this.contents = contents; |
| 126 | + } |
| 127 | + |
| 128 | + public AMQPFrame getMethod() { |
| 129 | + return method; |
| 130 | + } |
| 131 | + |
| 132 | + public void setMethod(AMQPFrame method) { |
| 133 | + this.method = method; |
| 134 | + } |
| 135 | + |
| 136 | + public AMQPFrame getHeader() { |
| 137 | + return header; |
| 138 | + } |
| 139 | + |
| 140 | + public void setHeader(AMQPFrame header) { |
| 141 | + this.header = header; |
| 142 | + } |
| 143 | + |
| 144 | + public List<AMQPFrame> getContents() { |
| 145 | + return contents; |
| 146 | + } |
| 147 | + |
| 148 | + public void setContents(List<AMQPFrame> contents) { |
| 149 | + this.contents = contents; |
| 150 | + } |
| 151 | + |
| 152 | + public void addContent(AMQPFrame content) { |
| 153 | + if (this.contents == null) { |
| 154 | + this.contents = new ArrayList<>(2); |
| 155 | + } |
| 156 | + contents.add(content); |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + } |
| 161 | +} |
0 commit comments