package com.curiousby.util;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.AbstractMessageConverter;
import org.springframework.amqp.support.converter.MessageConversionException;
import com.alibaba.fastjson.JSON;
//import fe.json.FastJson;
//com.curiousby.util.FastJsonMessageConverter
public class FastJsonMessageConverter extends AbstractMessageConverter {
private static Log log = LogFactory.getLog(FastJsonMessageConverter.class);
public static final String DEFAULT_CHARSET = "UTF-8";
private volatile String defaultCharset = DEFAULT_CHARSET;
public FastJsonMessageConverter() {
super();
}
public void setDefaultCharset(String defaultCharset) {
this.defaultCharset = (defaultCharset != null) ? defaultCharset
: DEFAULT_CHARSET;
}
public Object fromMessage(Message message)
throws MessageConversionException {
return null;
}
public <T> T fromMessage(Message message,T t) {
String json = "";
try {
json = new String(message.getBody(),defaultCharset);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return (T) JSON.parseObject(json, t.getClass());
//(T) FastJson.fromJson(json, t.getClass());
}
protected Message createMessage(Object objectToConvert,
MessageProperties messageProperties)
throws MessageConversionException {
byte[] bytes = null;
try {
String jsonString = JSON.toJSONString(objectToConvert);
//FastJson.toJson(objectToConvert);
bytes = jsonString.getBytes(this.defaultCharset);
} catch (UnsupportedEncodingException e) {
throw new MessageConversionException(
"Failed to convert Message content", e);
}
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
messageProperties.setContentEncoding(this.defaultCharset);
if (bytes != null) {
messageProperties.setContentLength(bytes.length);
}
return new Message(bytes, messageProperties);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
DemoRabbitMQ.rar (46个子文件)
DemoRabbitMQ
DemoRabbitMQ
.project 1KB
src
test
resources
java
com
curiousby
TestProducter.java 988B
MainStart.java 684B
TestConsumer.java 550B
main
webapp
WEB-INF
lib
web.xml 222B
index.jsp 57B
META-INF
MANIFEST.MF 39B
resources
log4j.xml 1KB
applicationContext.xml 3KB
applicationContext-rabbit-producer.xml 2KB
rabbitmq.properties 148B
applicationContext-rabbit-consumer.xml 2KB
java
com
curiousby
core
MessagePush.java 429B
MessageProcessListener.java 678B
util
FastJsonMessageConverter.java 2KB
test
entity
Message.java 2KB
target
maven-status
maven-compiler-plugin
compile
default-compile
createdFiles.lst 170B
inputFiles.lst 414B
generated-sources
annotations
classes
log4j.xml 1KB
applicationContext.xml 3KB
applicationContext-rabbit-producer.xml 2KB
com
curiousby
core
MessagePush.class 770B
MessageProcessListener.class 1KB
util
FastJsonMessageConverter.class 3KB
test
entity
Message.class 3KB
rabbitmq.properties 148B
applicationContext-rabbit-consumer.xml 2KB
DemoRabbitMQ.jar 11KB
maven-archiver
pom.properties 105B
test-classes
com
curiousby
TestProducter.class 2KB
MainStart.class 950B
TestConsumer.class 846B
DemoRabbitMQ-sources.jar 9KB
DemoRabbitMQ.iml 9KB
web
WEB-INF
web.xml 304B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 219B
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 736B
org.eclipse.wst.validation.prefs 50B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.core.resources.prefs 191B
org.eclipse.wst.common.component 766B
.jsdtscope 639B
pom.xml 11KB
.classpath 13KB
共 46 条
- 1
资源评论
curiousby
- 粉丝: 23
- 资源: 151
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功