有人可以帮我从PubSub-Message接收属性吗?消息显示正确但没有属性。我的接收类看起来像这样:
public class ReceiveMessageServlet extends HttpServlet {
@Override
public final void doPost(final HttpServletRequest req,
final HttpServletResponse resp)
throws IOException {
// Validating unique subscription token before processing the message
String subscriptionToken = System.getProperty(
Constants.BASE_PACKAGE + ".subscriptionUniqueToken");
if (!subscriptionToken.equals(req.getParameter("token"))) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().close();
return;
}
ServletInputStream inputStream = req.getInputStream();
// Parse the JSON message to the POJO model class
JsonParser parser = JacksonFactory.getDefaultInstance()
.createJsonParser(inputStream);
parser.skipToKey("message");
PubsubMessage message = parser.parseAndClose(PubsubMessage.class);
// Store the message in the datastore
Entity messageToStore = new Entity("PubsubMessage");
messageToStore.setProperty("message",
new String(message.decodeData(), "UTF-8"));
messageToStore.setProperty("receipt-time", System.currentTimeMillis());
DatastoreService datastore =
DatastoreServiceFactory.getDatastoreService();
datastore.put(messageToStore);
// Invalidate the cache
MemcacheService memcacheService =
MemcacheServiceFactory.getMemcacheService();
memcacheService.delete(Constants.MESSAGE_CACHE_KEY);
// Acknowledge the message by returning a success code
resp.setStatus(HttpServletResponse.SC_OK);
resp.getWriter().close();
}
}
我已经尝试使用message.getAttributes()但没有任何成功。
答案 0 :(得分:1)
您可能需要检查添加的PubsubIO功能,该功能允许Read
和Write
转换提供对latest release中添加的Cloud Pub / Sub消息属性的访问权限。< / p>
尝试使用withAttributes
方法。此,
使源返回包含Pubsub属性的PubsubMessage。用户必须提供解析函数以将PubsubMessage转换为输出类型。必须通过
PCollection.setCoder(Coder)
在输出上注册或设置输出类型T的编码器。
希望它有所帮助。