public static void publishData ( int numMessages , int delayInMillis ) throws IOException { List < PubsubMessage > pubsubMessages = new ArrayList <> ( ) ; for ( int i = 0 ; i < Math . max ( 1 , numMessages ) ; i ++ ) { Long currTime = System . currentTimeMillis ( ) ; String message = generateEvent ( currTime , delayInMillis ) ; PubsubMessage pubsubMessage = new PubsubMessage ( ) . encodeData ( message . getBytes ( "UTF-8" ) ) ; pubsubMessage . setAttributes ( ImmutableMap . of ( GameConstants . TIMESTAMP_ATTRIBUTE , Long . toString ( ( currTime - delayInMillis ) / 1000 * 1000 ) ) ) ; if ( delayInMillis != 0 ) { System . out . println ( pubsubMessage . getAttributes ( ) ) ; System . out . println ( "late data for: " + message ) ; } pubsubMessages . add ( pubsubMessage ) ; } PublishRequest publishRequest = new PublishRequest ( ) ; publishRequest . setMessages ( pubsubMessages ) ; pubsub . projects ( ) . topics ( ) . publish ( topic , publishRequest ) . execute ( ) ; }
Publish numMessages arbitrary events from live users with the provided delay to a PubSub topic .