1
- /*
2
- * Copyright 1999-2011 Alibaba Group.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
1
+ /*
2
+ * Copyright 1999-2011 Alibaba Group.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
16
package com .alibaba .dubbo .remoting .exchange ;
17
17
18
18
import java .util .concurrent .atomic .AtomicLong ;
19
19
20
+ import com .alibaba .dubbo .common .utils .StringUtils ;
21
+
20
22
/**
21
23
* Request.
22
24
*
23
25
* @author qian.lei
24
26
* @author william.liangf
25
27
*/
26
- public class Request {
27
-
28
- public static final String HEARTBEAT_EVENT = null ;
29
-
30
- public static final String READONLY_EVENT = "R" ;
31
-
28
+ public class Request {
29
+
30
+ public static final String HEARTBEAT_EVENT = null ;
31
+
32
+ public static final String READONLY_EVENT = "R" ;
33
+
32
34
private static final AtomicLong INVOKE_ID = new AtomicLong (0 );
33
35
34
36
private final long mId ;
35
37
36
38
private String mVersion ;
37
39
38
- private boolean mTwoWay = true ;
39
-
40
+ private boolean mTwoWay = true ;
41
+
40
42
private boolean mEvent = false ;
41
43
42
44
private boolean mBroken = false ;
@@ -70,15 +72,15 @@ public boolean isTwoWay() {
70
72
public void setTwoWay (boolean twoWay ) {
71
73
mTwoWay = twoWay ;
72
74
}
73
-
74
- public boolean isEvent () {
75
- return mEvent ;
76
- }
77
-
78
- public void setEvent (String event ) {
79
- mEvent = true ;
80
- mData = event ;
81
- }
75
+
76
+ public boolean isEvent () {
77
+ return mEvent ;
78
+ }
79
+
80
+ public void setEvent (String event ) {
81
+ mEvent = true ;
82
+ mData = event ;
83
+ }
82
84
83
85
public boolean isBroken () {
84
86
return mBroken ;
@@ -95,16 +97,16 @@ public Object getData() {
95
97
public void setData (Object msg ) {
96
98
mData = msg ;
97
99
}
98
-
99
- public boolean isHeartbeat () {
100
- return mEvent && HEARTBEAT_EVENT == mData ;
101
- }
102
-
103
- public void setHeartbeat (boolean isHeartbeat ) {
104
- if (isHeartbeat ) {
105
- setEvent (HEARTBEAT_EVENT );
106
- }
107
- }
100
+
101
+ public boolean isHeartbeat () {
102
+ return mEvent && HEARTBEAT_EVENT == mData ;
103
+ }
104
+
105
+ public void setHeartbeat (boolean isHeartbeat ) {
106
+ if (isHeartbeat ) {
107
+ setEvent (HEARTBEAT_EVENT );
108
+ }
109
+ }
108
110
109
111
private static long newId () {
110
112
// getAndIncrement()增长到MAX_VALUE时,再增长会变为MIN_VALUE,负数也可以做为ID
@@ -114,7 +116,18 @@ private static long newId() {
114
116
@ Override
115
117
public String toString () {
116
118
return "Request [id=" + mId + ", version=" + mVersion + ", twoway=" + mTwoWay + ", event=" + mEvent
117
- + ", broken=" + mBroken + ", data=" + (mData == this ? "this" : mData ) + "]" ;
119
+ + ", broken=" + mBroken + ", data=" + (mData == this ? "this" : safeToString ( mData ) ) + "]" ;
118
120
}
119
121
120
- }
122
+ private static String safeToString (Object data ) {
123
+ if (data == null ) return null ;
124
+ String dataStr ;
125
+ try {
126
+ dataStr = data .toString ();
127
+ } catch (Throwable e ) {
128
+ dataStr = "<Fail toString of " + data .getClass () + ", cause: " +
129
+ StringUtils .toString (e ) + ">" ;
130
+ }
131
+ return dataStr ;
132
+ }
133
+ }
0 commit comments