当前位置: 首页>>代码示例>>Java>>正文


Java PubsubMessage.encodeData方法代码示例

本文整理汇总了Java中com.google.api.services.pubsub.model.PubsubMessage.encodeData方法的典型用法代码示例。如果您正苦于以下问题:Java PubsubMessage.encodeData方法的具体用法?Java PubsubMessage.encodeData怎么用?Java PubsubMessage.encodeData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.api.services.pubsub.model.PubsubMessage的用法示例。


在下文中一共展示了PubsubMessage.encodeData方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: publishMessage

import com.google.api.services.pubsub.model.PubsubMessage; //导入方法依赖的package包/类
/**
 * Publishes the given message to the given topic.
 */
public void publishMessage(String message, String outputTopic) {
  int maxLogMessageLength = 200;
  if (message.length() < maxLogMessageLength) {
    maxLogMessageLength = message.length();
  }
  logger.info("Received ...." + message.substring(0, maxLogMessageLength));

  // Publish message to Pubsub.
  PubsubMessage pubsubMessage = new PubsubMessage();
  pubsubMessage.encodeData(message.getBytes());

  PublishRequest publishRequest = new PublishRequest();
  publishRequest.setTopic(outputTopic).setMessage(pubsubMessage);
  try {
    this.pubsub.topics().publish(publishRequest).execute();
  } catch (java.io.IOException e) {
    logger.warning(e.getStackTrace().toString());
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-pubsub-samples-java,代码行数:23,代码来源:NewsInjector.java

示例2: publishMessage

import com.google.api.services.pubsub.model.PubsubMessage; //导入方法依赖的package包/类
/**
 * Publishes the given message to a Cloud Pub/Sub topic.
 */
public void publishMessage(String message, String outputTopic) {
  int maxLogMessageLength = 200;
  if (message.length() < maxLogMessageLength) {
    maxLogMessageLength = message.length();
  }
  logger.info("Received ...." + message.substring(0, maxLogMessageLength));

  // Publish message to Pubsub.
  PubsubMessage pubsubMessage = new PubsubMessage();
  pubsubMessage.encodeData(message.getBytes());

  PublishRequest publishRequest = new PublishRequest();
  publishRequest.setTopic(outputTopic).setMessage(pubsubMessage);
  try {
    this.pubsub.topics().publish(publishRequest).execute();
  } catch (java.io.IOException e) {
    ;
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-pubsub-samples-java,代码行数:23,代码来源:StockInjector.java

示例3: doPost

import com.google.api.services.pubsub.model.PubsubMessage; //导入方法依赖的package包/类
@Override
public final void doPost(final HttpServletRequest req,
                         final HttpServletResponse resp)
        throws IOException {
    Pubsub client = PubsubUtils.getClient();
    String message = req.getParameter("message");
    if (!"".equals(message)) {
        String fullTopicName = String.format("projects/%s/topics/%s",
                PubsubUtils.getProjectId(),
                PubsubUtils.getAppTopicName());
        PubsubMessage pubsubMessage = new PubsubMessage();
        pubsubMessage.encodeData(message.getBytes("UTF-8"));
        PublishRequest publishRequest = new PublishRequest();
        publishRequest.setMessages(ImmutableList.of(pubsubMessage));

        client.projects().topics()
                .publish(fullTopicName, publishRequest)
                .execute();
    }
    resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
    resp.getWriter().close();
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-pubsub-samples-java,代码行数:23,代码来源:SendMessageServlet.java

示例4: processElement

import com.google.api.services.pubsub.model.PubsubMessage; //导入方法依赖的package包/类
@Override
public void processElement(ProcessContext c) throws IOException {
  if (c.element().isEmpty()) {
    return;
  }
  PubsubMessage pubsubMessage = new PubsubMessage();
  pubsubMessage.encodeData(c.element().getBytes());
  if (timestampLabelKey != null) {
    pubsubMessage.setAttributes(
        ImmutableMap.of(timestampLabelKey, Long.toString(c.timestamp().getMillis())));
  }
  PublishRequest publishRequest = new PublishRequest();
  publishRequest.setMessages(Arrays.asList(pubsubMessage));
  this.pubsub.projects().topics().publish(outputTopic, publishRequest).execute();
}
 
开发者ID:sinmetal,项目名称:iron-hippo,代码行数:16,代码来源:PubsubFileInjector.java

示例5: connectIrc

import com.google.api.services.pubsub.model.PubsubMessage; //导入方法依赖的package包/类
/**
 * Connects an IRC channel and publish the chat messages to the given topic.
 *
 * @param client Cloud Pub/Sub client.
 * @param args Command line arguments.
 * @throws IOException when Cloud Pub/Sub API calls fail.
 */
public static void connectIrc(final Pubsub client, final String[] args)
        throws IOException {
    Main.checkArgsLength(args, 5);
    final String server = args[3];
    final String channel = args[4];
    final String topic = PubsubUtils.getFullyQualifiedResourceName(
            PubsubUtils.ResourceType.TOPIC, args[0], args[2]);
    final String nick = String.format("bot-%s", args[0]);
    final Socket socket = new Socket(server, PORT);
    final BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream()));
    final BufferedReader reader = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));

    writer.write(String.format("NICK %s\r\n", nick));
    writer.write(String.format("USER %s 8 * : %s\r\n", nick, BOT_NAME));
    writer.flush();

    String line;
    while ((line = reader.readLine()) != null) {
        if (line.contains("004")) {
            System.out.printf("Connected to %s.\n", server);
            break;
        } else if (line.contains("433")) {
            System.err.println("Nickname is already in use.");
            return;
        }
    }

    writer.write(String.format("JOIN %s\r\n", channel));
    writer.flush();

    // A regex pattern for Wikipedia change log as of June 4, 2014
    Pattern pat = Pattern.compile(
            "\\u000314\\[\\[\\u000307(.*)\\u000314\\]\\]\\u0003.*"
                    + "\\u000302(http://[^\\u0003]*)\\u0003");
    while ((line = reader.readLine()) != null) {
        if (line.toLowerCase().startsWith("PING ")) {
            // We must respond to PINGs to avoid being disconnected.
            writer.write("PONG " + line.substring(5) + "\r\n");
            writer.write("PRIVMSG " + channel + " :I got pinged!\r\n");
            writer.flush();
        } else {
            String privmsgMark = "PRIVMSG " + channel + " :";
            int prividx = line.indexOf(privmsgMark);
            if (prividx == -1) {
                continue;
            }
            line = line.substring(prividx + privmsgMark.length(),
                                  line.length());
            PubsubMessage pubsubMessage = new PubsubMessage();
            Matcher matcher = pat.matcher(line);
            if (matcher.find()) {
                String message = String.format("Title: %s, Diff: %s",
                        matcher.group(1), matcher.group(2));
                pubsubMessage.encodeData(message.getBytes("UTF-8"));
            } else {
                pubsubMessage.encodeData(line.getBytes("UTF-8"));
            }
            List<PubsubMessage> messages = ImmutableList.of(pubsubMessage);
            PublishRequest publishRequest = new PublishRequest();
            publishRequest.setMessages(messages);
            client.projects().topics()
                    .publish(topic, publishRequest)
                    .execute();
        }
    }
}
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-pubsub-samples-java,代码行数:76,代码来源:TopicMethods.java


注:本文中的com.google.api.services.pubsub.model.PubsubMessage.encodeData方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。