如题所示,最开始使用了默认配置:
1
2
3
|
<
amq:connectionFactory
id
=
"amqConnectionFactory"
brokerURL
=
"tcp://${activemq.ip}:61616"
userName
=
"${activemq.username}"
password
=
"${activemq.passwd}"
/>
|
然后使用ActiveMQ对对象进行序列化时报了如下错误:
1
2
3
4
5
6
7
8
9
10
|
Caused by: java.lang.ClassNotFoundException: Forbidden class cn.zifangsky.model.User! This class is not trusted to be serialized as ObjectMessage payload. Please take a look at http://activemq.apache.org/objectmessage.html for more information on how to configure trusted classes.
at org.apache.activemq.util.ClassLoadingAwareObjectInputStream.checkSecurity(ClassLoadingAwareObjectInputStream.java:112)
at org.apache.activemq.util.ClassLoadingAwareObjectInputStream.resolveClass(ClassLoadingAwareObjectInputStream.java:57)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at org.apache.activemq.command.ActiveMQObjectMessage.getObject(ActiveMQObjectMessage.java:206)
... 13 more
|
关于这个错误,其实在报错的提示里面已经给出了官方的解决方案的地址,即:http://activemq.apache.org/objectmessage.html
出现这个问题的原因在于:从ActiveMQ5.12.2 开始,为了增强这个框架的安全性,ActiveMQ将强制用户配置可序列化的包名。因此具体的解决方案如下:
按照官方的提示修改“ActiveMQ 连接工厂”的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<
bean
id
=
"amqConnectionFactory"
class
=
"org.apache.activemq.spring.ActiveMQConnectionFactory"
>
<
property
name
=
"brokerURL"
value
=
"tcp://${activemq.ip}:61616"
/>
<
property
name
=
"userName"
value
=
"${activemq.username}"
/>
<
property
name
=
"password"
value
=
"${activemq.passwd}"
/>
<
property
name
=
"trustedPackages"
>
<
list
>
<
value
>java.lang</
value
>
<
value
>javax.security</
value
>
<
value
>java.util</
value
>
<
value
>org.apache.activemq</
value
>
<
value
>cn.zifangsky.activemq</
value
>
<
value
>cn.zifangsky.model</
value
>
</
list
>
</
property
>
</
bean
>
|
这里添加上信任的可序列化的包即可
如果不想一个个地添加的话,也可以使用“trustAllPackages”参数:
1
2
3
4
5
6
|
<
bean
id
=
"amqConnectionFactory"
class
=
"org.apache.activemq.spring.ActiveMQConnectionFactory"
>
<
property
name
=
"brokerURL"
value
=
"tcp://${activemq.ip}:61616"
/>
<
property
name
=
"userName"
value
=
"${activemq.username}"
/>
<
property
name
=
"password"
value
=
"${activemq.passwd}"
/>
<
property
name
=
"trustAllPackages"
value
=
"true"
/>
</
bean
>
|
注:官方还提示可以给activemq.bat文件(PS:D:/apache-activemq-5.14.1/bin/activemq.bat)添加信任的包名。也就是修改文件中的“ACTIVEMQ_OPTS”参数,在这行参数后面添加如下的配置:
1
|
-Dorg.apache.activemq.SERIALIZABLE_PACKAGES=java.lang,javax.security,java.util,org.apache.activemq,cn.zifangsky.activemq,cn.zifangsky.model
|
但是经过尝试我发现并没有效果,其实仅仅只是修改“ActiveMQ 的连接工厂”那段配置就可以解决这个问题了
最后测试效果如下:
1
2
3
4
5
6
|
@Test
public
void
testObject(){
User u =
new
User((
long
)
1
,
"test"
,
"123456"
);
queueSender2.send(
"object.queue"
, u);
}
|
运行这个方法之后,输出如下:
1
|
接收到消息: User [id=1, username=test, password=123456]
|
可以发现,问题成功解决了
本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1877600,如需转载请自行联系原作者