package com.yannis.utils;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.namespace.QName;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class JaxbMa服务器托管网pper {
private static ConcurrentMap jaxbContexts = new ConcurrentHashMap();
public static String toXml(Object root) {
return toXml(root, root.getClass(), null);
}
public static String toXml(Object root, String encoding) {
return toXml(root, root.getClass(), encoding);
}
// @SneakyThrows(JAXBException.class)
public static String toXml(Object root, Class clazz, String encoding){
String s = "";
try {
StringWriter writer = new StringWriter();
createMarshaller(clazz, encoding).marshal(root, writer);
s = writer.toString();
} catch (Exception e) {
}
return s;
}
public static String toXml(Collection> root, String rootName, Class clazz) {
return toXml(root, rootName, clazz, null);
}
public static String toXml(Collection> root, String rootName, Class clazz, String encoding) {
String string = "";
try {
CollectionWrapper wrapper = new CollectionWrapper();
wrapper.collection = root;
JAXBElement wrapperElement = new JAXBElement(new QName(rootName),
CollectionWrapper.class, wrapper);
StringWriter writer = new StringWriter();
createMarshaller(clazz, encoding).marshal(wrapperElement, writer);
string = writer.toString();
} catch (Exception e) {
}
return string;
}
@SuppressWarnings("unchecked")
public static T fromXml(String xml, Class clazz) throws Exception{
StringReader reader = new StringReader(xml);
return (T) createUnmarshaller(clazz).unmarshal(reader);
}
/**
* 创建Marshaller并设定encoding(可为null).
* 线程不安全,需要每次创建或pooling。
*/
public static Marshaller createMarshaller(Class clazz, String encoding) throws Exception{
JAXBContext jaxbContext = getJaxbContext(clazz);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
if (encoding != null) {
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
}
return marshaller;
}
/**
* 创建UnMarshaller.
* 线程不安全,需要每次创建或pooling。
*/
public static Unmarshaller createUnmarshaller(Class clazz) throws Exception{
JAXBContext jaxbContext = getJaxbContext(clazz);
return jaxbContext.createUnmarshaller();
}
protected static JAXBContext ge服务器托管网tJaxbContext(Class clazz) throws Exception{
// Assert.notNull(clazz, "'clazz' must not be null");
JAXBContext jaxbContext = jaxbContexts.get(clazz);
if (jaxbContext == null) {
jaxbContext = JAXBContext.newInstance(clazz, CollectionWrapper.class);
jaxbContexts.putIfAbsent(clazz, jaxbContext);
}
return jaxbContext;
}
/**
* 封装Root Element 是 Collection的情况.
*/
public static class CollectionWrapper {
@XmlAnyElement
protected Collection> collection;
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
当前OPPO的数据中心中已经有一定数量支持RDMA的网卡(包含IB及ROCEv2),除了机器学习场景以外,之前的文章ORPC[1] 也已经分享了OPPO在RPC over RDMA传输的实践,具体RDMA相关前置知识也可以参考此篇文章。为了充分发挥RDMA低延…