-
Notifications
You must be signed in to change notification settings - Fork 910
/
Channel.java
271 lines (234 loc) · 8.35 KB
/
Channel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.activemq.artemis.core.protocol.core;
import java.util.concurrent.locks.Lock;
import org.apache.activemq.artemis.api.core.ActiveMQException;
/**
* A channel is a way of interleaving data meant for different endpoints over the same {@link org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection}.
* <p>
* Any packet sent will have its channel id set to the specific channel sending so it can be routed to its correct channel
* when received by the {@link org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection}. see {@link org.apache.activemq.artemis.core.protocol.core.Packet#setChannelID(long)}.
* <p>
* Each Channel should will forward any packets received to its {@link org.apache.activemq.artemis.core.protocol.core.ChannelHandler}.
* <p>
* A Channel *does not* support concurrent access by more than one thread!
*/
public interface Channel {
/**
* Returns the id of this channel.
*
* @return the id
*/
long getID();
/**
* This number increases every time the channel reconnects successfully.
* This is used to guarantee the integrity of the channel on sequential commands such as large messages.
*
* @return
*/
int getReconnectID();
/**
* For protocol check
*/
boolean supports(byte packetID);
/**
* For protocol check
*/
boolean supports(byte packetID, int version);
/**
* Sends a packet on this channel.
*
* @param packet the packet to send
* @return false if the packet was rejected by an outgoing interceptor; true if the send was
* successful
*/
boolean send(Packet packet);
/**
* Sends a packet on this channel.
*
* @param packet the packet to send
* @return false if the packet was rejected by an outgoing interceptor; true if the send was
* successful
*/
boolean send(Packet packet, int reconnectID);
/**
* Sends a packet on this channel using batching algorithm if appropriate
*
* @param packet the packet to send
* @return false if the packet was rejected by an outgoing interceptor; true if the send was
* successful
*/
boolean sendBatched(Packet packet);
/**
* Similarly to {@code flushConnection} on {@link #send(Packet, boolean)}, it requests
* any un-flushed previous sent packets to be flushed to the underlying connection.<br>
* It can be a no-op in case of InVM transports, because they would likely to flush already on each send.
*/
void flushConnection();
/**
* Sends a packet on this channel, but request it to be flushed (along with the un-flushed previous ones) only iff
* {@code flushConnection} is {@code true}.
*
* @param packet the packet to send
* @param flushConnection if {@code true} requests this {@code packet} and any un-flushed previous sent one to be flushed
* to the underlying connection
* @return false if the packet was rejected by an outgoing interceptor; true if the send was
* successful
*/
boolean send(Packet packet, boolean flushConnection);
/**
* Sends a packet on this channel and then blocks until it has been written to the connection.
*
* @param packet the packet to send
* @return false if the packet was rejected by an outgoing interceptor; true if the send was
* successful
*/
boolean sendAndFlush(Packet packet);
/**
* Sends a packet on this channel and then blocks until a response is received or a timeout
* occurs.
*
* @param packet the packet to send
* @param expectedPacket the packet being expected.
* @return the response
* @throws ActiveMQException if an error occurs during the send
*/
Packet sendBlocking(Packet packet, byte expectedPacket) throws ActiveMQException;
/**
* Sends a packet on this channel and then blocks until a response is received or a timeout
* occurs.
*
* @param packet the packet to send
* @param expectedPacket the packet being expected.
* @return the response
* @throws ActiveMQException if an error occurs during the send
*/
Packet sendBlocking(Packet packet, int reconnectID, byte expectedPacket) throws ActiveMQException;
/**
* Sets the {@link org.apache.activemq.artemis.core.protocol.core.ChannelHandler} that this channel should
* forward received packets to.
*
* @param handler the handler
*/
void setHandler(ChannelHandler handler);
/**
* Gets the {@link org.apache.activemq.artemis.core.protocol.core.ChannelHandler} that this channel should
* forward received packets to.
*
* @return the current channel handler
*/
ChannelHandler getHandler();
void endOfBatch();
/**
* Closes this channel.
* <p>
* once closed no packets can be sent.
*/
void close();
/**
* Transfers the connection used by this channel to the one specified.
* <p>
* All new packets will be sent via this connection.
*
* @param newConnection the new connection
*/
void transferConnection(CoreRemotingConnection newConnection);
/**
* resends any packets that have not received confirmations yet.
* <p>
* Typically called after a connection has been transferred.
*
* @param lastConfirmedCommandID the last confirmed packet
*/
void replayCommands(int lastConfirmedCommandID);
/**
* returns the last confirmed packet command id
*
* @return the id
*/
int getLastConfirmedCommandID();
/**
* locks the channel.
* <p>
* While locked no packets can be sent or received
*/
void lock();
/**
* unlocks the channel.
*/
void unlock();
/**
* forces any {@link org.apache.activemq.artemis.core.protocol.core.Channel#sendBlocking(Packet, byte)} request to return with an exception.
*/
void returnBlocking();
/**
* forces any {@link org.apache.activemq.artemis.core.protocol.core.Channel#sendBlocking(Packet, byte)} request to return with an exception.
*/
void returnBlocking(Throwable cause);
/**
* returns the channel lock
*
* @return the lock
*/
Lock getLock();
/**
* returns the {@link CoreRemotingConnection} being used by the channel
*/
CoreRemotingConnection getConnection();
/**
* sends a confirmation of a packet being received.
*
* @param packet the packet to confirm
*/
void confirm(Packet packet);
/**
* sets the handler to use when a confirmation is received.
*
* @param handler the handler to call
*/
void setCommandConfirmationHandler(CommandConfirmationHandler handler);
void setResponseHandler(ResponseHandler handler);
/**
* flushes any confirmations on to the connection.
*/
void flushConfirmations();
/**
* Called by {@link org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection} when a packet is received.
* <p>
* This method should then call its {@link org.apache.activemq.artemis.core.protocol.core.ChannelHandler} after appropriate processing of
* the packet
*
* @param packet the packet to process.
*/
void handlePacket(Packet packet);
/**
* clears any commands from the cache that are yet to be confirmed.
*/
void clearCommands();
/**
* returns the confirmation window size this channel is using.
*
* @return the window size
*/
int getConfirmationWindowSize();
/**
* notifies the channel if it is transferring its connection. When true it is illegal to send messages.
*
* @param transferring whether the channel is transferring
*/
void setTransferring(boolean transferring);
}