本文整理汇总了Java中com.google.protobuf.AbstractMessage类的典型用法代码示例。如果您正苦于以下问题:Java AbstractMessage类的具体用法?Java AbstractMessage怎么用?Java AbstractMessage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AbstractMessage类属于com.google.protobuf包,在下文中一共展示了AbstractMessage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decodeJson
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
public static WsPacket decodeJson(String stringResult) {
try {
JSONObject jsObj = JSONObject.fromObject(stringResult);
String wsOpCode = jsObj.getString(WSOPCODE);
if (wsOpCode == null) {
if (WSManager.log != null) {
WSManager.log.warn("数据为:" + stringResult + ",无wsOpCode");
}
return null;
}
if (!WSManager.wsOpCodeMap.containsKey(wsOpCode)) {
if (WSManager.log != null) {
WSManager.log.warn("wsOpCode为:" + wsOpCode + "无对应解析,请及时解决");
}
return null;
}
Class<?> className = WSManager.wsOpCodeMap.get(wsOpCode);
Method buildM = className.getDeclaredMethod("newBuilder");
AbstractMessage.Builder<?> builder = (Builder<?>) buildM.invoke(null);
Message data = PacketUtils.jsonToProtoBuf(stringResult, builder);
WsPacket wsPacket = new WsPacket(wsOpCode, data);
return wsPacket;
} catch (Exception e) {
if (WSManager.log != null) {
WSManager.log.error("json转换成protobuf异常", e);
}
return null;
}
}
示例2: decode
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
// int占4个字节
int messageId = in.readInt();
// 消息体的长度(假设长度为20)
byte[] bytes = new byte[in.readableBytes()];
in.readBytes(bytes);
// 4+20 =24
AbstractMessage message = msgPool.getMessage(messageId);
if (message == null) {
LOGGER.error("未注册的消息id{}", messageId);
return;
}
out.add(message.getParserForType().parseFrom(bytes));
}
示例3: encode
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
@Override
protected void encode(ChannelHandlerContext ctx, AbstractMessage msg, ByteBuf out) throws Exception {
int messageId = msgPool.getMessageId(msg);
if (messageId == 0) {
LOGGER.error("编码到未知的消息{}", messageId);
}
byte[] bytes = msg.toByteArray();
int length = Integer.BYTES + bytes.length;
boolean writeAble = out.isWritable(length);
if (!writeAble) {
LOGGER.error("消息过大,编码失败 {} -> {}", messageId, length);
return;
}
// int->4
out.writeInt(messageId);
// ->20(假设)
out.writeBytes(bytes);
}
示例4: requireAllFieldsExcept
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
private static void requireAllFieldsExcept(AbstractMessage message, int... fieldNumbersNotRequired) {
Collection<Descriptors.FieldDescriptor> required = new ArrayList<>(message.getDescriptorForType().getFields());
Collection<Descriptors.FieldDescriptor> actual = message.getAllFields().keySet();
required.removeAll(actual);
if(fieldNumbersNotRequired != null) {
for(int fieldNumber : fieldNumbersNotRequired) {
required.remove(message.getDescriptorForType().findFieldByNumber(fieldNumber));
}
}
if(!required.isEmpty()) {
Collection<String> names = new ArrayList<>(required.size());
for(Descriptors.FieldDescriptor desc : required) {
names.add(desc.getName());
}
throw new ProtobufReadException(message.getDescriptorForType().getFullName(),
"Missing required fields: " + names.toString());
}
}
示例5: parseDelimitedFrom
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
public static <T extends /*@NonNull*/ AbstractMessage> List<T> parseDelimitedFrom(
@Nullable ByteBuffer byteBuf, Parser<T> parser) throws IOException {
if (byteBuf == null) {
return ImmutableList.of();
}
SizeLimitBypassingParser<T> sizeLimitBypassingParser =
new SizeLimitBypassingParser<>(parser);
List<T> messages = Lists.newArrayList();
try (InputStream input = new ByteBufferInputStream(byteBuf)) {
T message;
while ((message = sizeLimitBypassingParser.parseDelimitedFrom(input)) != null) {
messages.add(message);
}
}
return messages;
}
示例6: send
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
@Override
public void send(final MessageController messageController, final AbstractMessage message) {
final AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
try {
final Map<String, Collection<String>> headers = buildHeaders(messageController);
final AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.preparePost(url);
requestBuilder.setHeaders(headers);
requestBuilder.setBody(messagePayload(messageController, message));
final Future<Response> f = requestBuilder.execute();
final Response r = f.get();
final String responseBody = r.getResponseBody();
if (!"OK".equals(responseBody)) {
LOGGER.warn("Non-OK response received for HTTP transport client request.");
}
} catch (final IOException | ExecutionException | InterruptedException e) {
LOGGER.error("Could not execute HTTP request.", e);
}
}
示例7: send
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
@Override
public void send(final MessageController messageController, final AbstractMessage message) {
sendCount.increment();
final ServiceProto.ContentType contentType = getContentType(messageController);
if (contentType == null) {
sendFailureCount.increment();
throw new RuntimeException("Could not get content type of message.");
}
try {
final byte[] messageBytes = rawMessageBytes(contentType, message);
final AMQP.BasicProperties basicProperties = buildBasicProperties(messageController, contentType);
channel.basicPublish(
transportRef.getExchange(),
"fanout".equals(transportRef.getType()) ? "" : transportRef.getRoutingKey(),
basicProperties,
messageBytes);
} catch (final Exception e) {
sendFailureCount.increment();
LOGGER.error("Exception caught publishing message:", e);
}
}
示例8: sendAndReceive
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
@Override
public Future<Message> sendAndReceive(
final ServiceProto.ServiceRef destination,
final AbstractMessage message,
final Class messageClass,
final Optional<Expiration> expirationOptional) {
final SettableFuture<Message> resultsFuture = SettableFuture.create();
final byte[] messageId = MessageUtils.randomMessageId(24);
final MessageController controller =
new DefaultMessageController(
serviceRef,
destination,
ContentTypes.fromClass(messageClass),
Optional.of(messageId),
Optional.<byte[]> absent(),
expirationOptional.isPresent() ? Optional.of(expirationOptional.get().getExpiration()) : Optional
.<DateTime> absent());
resultsFutures.putIfAbsent(Arrays.hashCode(messageId), new Pair<>(resultsFuture, DateTime.now()));
serviceRegistry.sendMessage(controller, message);
return resultsFuture;
}
示例9: build
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
public NettyRpcProto.RpcRequest build() {
NettyRpcProto.RpcRequest result = buildPartial();
if (!result.isInitialized()) {
throw AbstractMessage.Builder.newUninitializedMessageException(result);
}
return result;
}
示例10: decodeJson
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
/**
* 将字符串转换成HttpPacket
*
* @param stringResult
* 字符串
* @param isServer
* 是不是服务器一般传true
* @param httpPacket
* 消息包
* @return
*/
public static boolean decodeJson(String stringResult, boolean isServer, HttpPacket httpPacket) {
try {
// 转换成json获取hOpCode,如果没有看看头消息有没有
JSONObject jsObj = JSONObject.fromObject(stringResult);
String hOpCode;
if (jsObj.containsKey(AllowParam.HOPCODE)) {
hOpCode = jsObj.getString(AllowParam.HOPCODE);
} else if (httpPacket.hSession.headParam.hOpCode != null && !httpPacket.hSession.headParam.hOpCode.equals("")) {
hOpCode = httpPacket.hSession.headParam.hOpCode;
} else {
return false;
}
// 是否设定相应解析
if (!HttpManager.hOpCodeMap.containsKey(hOpCode)) {
if (HttpConfig.log != null) {
HttpConfig.log.warn("hOpCode为:" + hOpCode + "无对应解析,请及时解决");
}
return false;
}
// 解析
Class<?>[] classNames = HttpManager.hOpCodeMap.get(hOpCode);
Class<?> className;
if (isServer) {
className = classNames[0];
} else {
className = classNames[1];
}
Method buildM = className.getDeclaredMethod("newBuilder");
AbstractMessage.Builder<?> builder = (Builder<?>) buildM.invoke(null);
Message data = PacketUtils.jsonToProtoBuf(stringResult, builder);
if (data == null) {
return false;
}
// 设置hOpCode和消息体
httpPacket.sethOpCode(hOpCode);
httpPacket.setData(data);
return true;
} catch (Exception e) {
if (HttpConfig.log != null) {
HttpConfig.log.error("json转换成protobuf异常", e);
}
return false;
}
}
示例11: consume
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public void consume(AbstractMessage msg, Channel channel) {
//将消息分发到指定的队列中,该队列有可能在同一个进程,也有可能不在同一个进程
int queueId = 1;
IProcessor processor = processors.get(queueId);
if (processor == null) {
log.error("找不到可用的消息处理器[{}]", queueId);
return;
}
Session session = AttributeUtil.get(channel, SessionKey.SESSION);
if (session == null) {
return;
}
AbstractHandler handler = msgPool.getHandler(msg.getClass().getName());
handler.setMessage(msg);
handler.setParam(session);
log.debug("收到消息:" + msg);
processor.process(handler);
}
示例12: build
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
public Row build() {
Row result = buildPartial();
if (!result.isInitialized()) {
throw AbstractMessage.Builder.newUninitializedMessageException(result);
}
return result;
}
示例13: build
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
public HColumn build() {
HColumn result = buildPartial();
if (!result.isInitialized()) {
throw AbstractMessage.Builder.newUninitializedMessageException(result);
}
return result;
}
示例14: consume
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public void consume(AbstractMessage msg, Channel channel) {
//将消息分发到指定的队列中,该队列有可能在同一个进程,也有可能不在同一个进程
int queueId = 1;
IProcessor processor = processors.get(queueId);
if (processor == null) {
LOGGER.error("找不到可用的消息处理器[{}]", queueId);
return;
}
Session session = AttributeUtil.get(channel, SessionKey.SESSION);
if (session == null) {
return;
}
AbstractHandler handler = msgPool.getHandler(msg.getClass().getName());
handler.setMessage(msg);
handler.setParam(session);
LOGGER.debug("收到消息:" + msg);
processor.process(handler);
}
示例15: sendMsg
import com.google.protobuf.AbstractMessage; //导入依赖的package包/类
public static void sendMsg(AbstractMessage msg, long id) {
Session session = SessionManager.getInstance().getSession(id);
if (session == null) {
return;
}
session.sendMessage(msg);
}