特殊类型兼容
dboss需要考虑参数值的各种可能性。
0. server端返回 boolean类型需要处理
1. 各种null, 注意null可能是private类型(不允许null),基本对象类型(String,Long),符合类型(javaBean),map,list。
python client 构造query处理,如果是None则是空字符:
def __invoke(self, method, params): query = [] i = 1; for param in params: param = param and json.dumps(param,cls=DbossObjectEncoder) or '' query.append(("param%s"%i,param)) i+=1
java server 判断如果是空字符,则put null对象
public RequestImpl(String deal) { for(String paramEntry:paramSplit){ int eq_idx = paramEntry.indexOf('='); String key = paramEntry.substring(0,eq_idx); String value = paramEntry.substring(eq_idx+1); try { if(value!=null&&!value.equals("")){ parameters.put(key, URLDecoder.decode(value,"UTF-8")); }else{ parameters.put(key, null); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } }
1. jsonParse如果发现是null则设置为null
while ((jsonToken = jsonParser.nextToken()) != JsonToken.END_OBJECT) { String key = jsonParser.getText(); JsonToken nextToken = jsonParser.nextToken(); if (isSimpleValue(nextToken)) { if (nextToken != JsonToken.VALUE_NULL) { // 注意处理null String value = jsonParser.getText(); object.put(key, value); }else{ object.put(key, null); } } else { ValueBean vb = parse(jsonParser); Object value = vb.isObject() ? vb.getObject() : vb.getArray(); object.put(key, value); } }
2. DataResolver 在处理之前判断是否是null,如果是null则退出
测试用例:
1. 复合参数直接是None
# -*- coding: utf-8 -*- from django.core.management import setup_environ import settings setup_environ(settings) from common.coreservice2 import searchService p = None page =searchService.testItemQuery(p) print page
2. 复合属性是None
# -*- coding: utf-8 -*- from django.core.management import setup_environ import settings setup_environ(settings) from common.coreservice2 import searchService p = {} p["tagSet"] = ['mp3'] p["minPrice"] = '1' p["maxPrice"] = None p["page"] = '1' p["page_size"] = '10' page =searchService.testItemQuery(p) print page
2. datetime。
json:
objectMapper = new ObjectMapper(jsonFactory); objectMapper.setSerializerProvider(sp); objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); // 使用兼容python的日期格式化 objectMapper.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false); objectMapper.setDateFormat(new PythonDateFormat());
基本类型:
public String deresolve(Object obj) { if (obj == null) { return ""; } if (obj instanceof Date) { return DateUtil.toPythonDate((Date) obj); } else { return obj.toString(); } }
/** * ..java dateformat 1349752845811 <br> * python dateformat 1349752935.191589 * * @author yunpeng */ public class DateUtil { public static String toPythonDate(Date date) { String datetime = String.valueOf(date.getTime()); return datetime.substring(0, datetime.length() - 3) + "." + datetime.substring(datetime.length() - 3) + "000"; } public static Date toJavaDate(String value) { int index = value.indexOf('.'); if (index != -1) { value = value.substring(0, index) + "000"; } return new Date(Long.parseLong(value)); } public static void main(String[] args) { System.out.println(DateUtil.toPythonDate(DateUtil.toJavaDate("1349755037.918218"))); } }
测试用例:
from datetime import datetime t = float(searchService.toDates(datetime.now())[0]) print t print datetime.fromtimestamp(t) t = float(searchService.toDate(datetime.now())) print t print datetime.fromtimestamp(t)
3. set。
4. boolean值。
连接池控制
有时候因为server端故障导致响应很慢,这时需要对最大connection做控制,否则会打开过多的connection,比如下面这张截图是一次由于mongoDB index重建导致某一台memcached server很慢,dboss connection爆涨:
server端异常控制
如果java端报错,dboss会返回完整的错误信息给python:
server中服务如果不存在不会报错
dict中的属性为None的情况
url=dboss://searchService/queryItem?param1={'page_size': 24, 'page': 1, 'tagSet': None}
1. dboss无法区分" "和null
2. dboss 无法处理\r\n
3. json溢出问题