# Step 3: Publish Events and Verify the Managed Subscription

Publish events by using the `PublishStream` RPC. Then verify that the subscription resumes after the last received event after restarting it and calling `ManagedSubscribe` RPC with the same managed subscription ID.

:::note
The `ManagedSubscribe` RPC method is part of a Beta Service. Customer may opt to try such Beta Service in its sole discretion. Any use of the Beta Service is subject to the applicable Beta Services Terms provided at [Agreements and Terms](https://www.salesforce.com/company/legal/agreements/).
:::

1. In `arguments.yaml`, supply these arguments.
   - `SINGLE_PUBLISH_REQUEST: true`: This argument specifies that all events are sent in one publish request.
   - `TOPIC: /event/Order_Event__e`: This argument specifies the topic for event publishing.
2. Modify the publishing code to specify the event fields and values to publish. In `java/src/main/java/utility/CommonContext.java`, modify the `public List<GenericRecord> createEventMessages(Schema schema, final int numEvents)` method. Replace the `<User_Id>` placeholder value of the `CreatedById` field with a valid user ID in your Salesforce org. To get the user ID, see [Find the Salesforce ID for a User or Profile](https://help.salesforce.com/s/articleView?id=000381643\&type=1).

```
.set("CreatedById", "<User_Id>")
```

3. In a second terminal window, go to the top-level `java` folder in your `pub-sub-api` folder.

4. To build the Java client, enter `mvn clean install`.

5. To run the `PublishStream` RPC example, enter `./run.sh genericpubsub.PublishStream`.

   The example publishes five events as set in the `arguments.yaml` default configuration. It prints out logging information to the terminal.

6. Return to the terminal for the `ManagedSubscribe` client, and verify that the logging output shows the five events received. Also, the log output shows that a commit request is sent after the batch of events is received to commit the Replay ID. The output looks similar to this output.

```
2024-06-03 09:12:30,942 [main] java.lang.Class - Using grpcHost api.pubsub.salesforce.com and grpcPort 7443
2024-06-03 09:12:31,524 [main] java.lang.Class - Starting managed subscription
with developer name My_Managed_Subscription
2024-06-03 09:12:31,738 [grpc-nio-worker-ELG-1-2] utility.APISessionCredentials -
Client Trace Id for current request: cd5faf97-8fa1-4efe-a770-d6166067b922
2024-06-03 09:12:36,531 [main] java.lang.Class - Subscription Active. Received a total of 0 events.
2024-06-03 09:12:41,535 [main] java.lang.Class - Subscription Active. Received a total of 0 events.
2024-06-03 09:12:44,520 [grpc-default-executor-1] java.lang.Class - ManagedFetchResponse batch
of 5 events pending requested: 0
2024-06-03 09:12:44,520 [grpc-default-executor-1] java.lang.Class - RPC ID: e5219940-7133-b6a4-97a6-9beabbc7573e
2024-06-03 09:12:44,520 [grpc-default-executor-1] java.lang.Class - processEvent -
EventID: f23132b9-7df6-446b-8fe6-2b7cee811376 SchemaId: _CADE3qaegX3ECEW0rlbYA
2024-06-03 09:12:44,524 [grpc-default-executor-1] utility.APISessionCredentials -
Client Trace Id for current request: 0c1cbc1c-1189-40fe-8b9b-cc64c333e530
2024-06-03 09:12:44,685 [grpc-default-executor-1] java.lang.Class - Received event:
{
  "CreatedDate": 1717431164101,
  "CreatedById": "005SG000009B3S1YAK",
  "Order_Number__c": "99",
  "City__c": "Los Angeles",
  "Amount__c": 35.0
}
2024-06-03 09:12:44,685 [grpc-default-executor-1] java.lang.Class - processEvent -
EventID: 981d92b0-179f-40f4-be72-fc93ae8a3660 SchemaId: _CADE3qaegX3ECEW0rlbYA
2024-06-03 09:12:44,685 [grpc-default-executor-1] java.lang.Class - Received event:
{
  "CreatedDate": 1717431164101,
  "CreatedById": "005SG000009B3S1YAK",
  "Order_Number__c": "100",
  "City__c": "New York",
  "Amount__c": 20.0
}
2024-06-03 09:12:44,685 [grpc-default-executor-1] java.lang.Class - processEvent -
EventID: 94b4b89a-9158-4d4b-beaa-63e3555c4cbd SchemaId: _CADE3qaegX3ECEW0rlbYA
2024-06-03 09:12:44,685 [grpc-default-executor-1] java.lang.Class - Received event:
{
  "CreatedDate": 1717431164101,
  "CreatedById": "005SG000009B3S1YAK",
  "Order_Number__c": "101",
  "City__c": "San Francisco",
  "Amount__c": 2.0
}
2024-06-03 09:12:44,685 [grpc-default-executor-1] java.lang.Class - processEvent -
EventID: dc37a809-542b-4696-b6f5-bede7a7733d0 SchemaId: _CADE3qaegX3ECEW0rlbYA
2024-06-03 09:12:44,685 [grpc-default-executor-1] java.lang.Class - Received event:
{
  "CreatedDate": 1717431164101,
  "CreatedById": "005SG000009B3S1YAK",
  "Order_Number__c": "102",
  "City__c": "San Jose",
  "Amount__c": 123.0
}
2024-06-03 09:12:44,685 [grpc-default-executor-1] java.lang.Class - processEvent -
EventID: 102bedd7-df2c-4daf-b7c9-e25acde4be47 SchemaId: _CADE3qaegX3ECEW0rlbYA
2024-06-03 09:12:44,685 [grpc-default-executor-1] java.lang.Class - Received event:
{
  "CreatedDate": 1717431164101,
  "CreatedById": "005SG000009B3S1YAK",
  "Order_Number__c": "103",
  "City__c": "Boston",
  "Amount__c": 180.0
}
2024-06-03 09:12:44,685 [grpc-default-executor-1] java.lang.Class - Processed batch of 5 event(s)
2024-06-03 09:12:44,686 [grpc-default-executor-1] java.lang.Class - Sending CommitRequest with
CommitReplayRequest ID: bd557942-4fd1-4699-be18-70db3054b33c
2024-06-03 09:12:44,686 [grpc-default-executor-1] java.lang.Class - Fetching more events: 5
2024-06-03 09:12:44,686 [main] java.lang.Class - Subscription Active. Received a total of 5 events.
2024-06-03 09:12:44,795 [grpc-default-executor-1] java.lang.Class - ManagedFetchResponse batch
of 0 events pending requested: 5
2024-06-03 09:12:44,796 [grpc-default-executor-1] java.lang.Class - RPC ID: e5219940-7133-b6a4-97a6-9beabbc7573e
2024-06-03 09:12:44,796 [grpc-default-executor-1] java.lang.Class - Successfully committed replay
with CommitRequestId: bd557942-4fd1-4699-be18-70db3054b33c with process time: 1717431164709
2024-06-03 09:12:44,796 [main] java.lang.Class - Subscription Active. Received a total of 5 events.
```

7. To disconnect the `ManagedSubscribe` client, press Ctrl+C in the terminal.

8. Publish more events while the subscriber is disconnected.

   a. To differentiate the events that are published next from the previous events, modify the Order Number field values so that they’re higher than the previous numbers. In `java/src/main/java/utility/CommonContext.java`, in the `createEventMessages(Schema schema, final int numEvents)` method, replace the `orderNumbers` array with this statement.

   ```
   String[] orderNumbers = {"104","105","106","107","108"};
   ```

   b. Because you modified the publishing code, rebuild the Pub/Sub API Java client. From the top-level java folder, enter `mvn clean install`.

   c. In the `PublishStream` client terminal, run the client by entering `./run.sh genericpubsub.PublishStream`.

   d. Verify that all five new events are published.

9. Return to the `ManagedSubscribe` client terminal. Restart the managed subscription by entering `./run.sh genericpubsub.ManagedSubscribe`.

10. The subscription resumes after the last received event, starting with the missed events. The `ManagedSubscribe` client output shows all the remaining events received with order numbers `104` through `108`. Also, the log output shows that a commit request is sent after the batch of events is received to commit the Replay ID.

```
2024-06-03 09:14:09,931 [main] java.lang.Class - Using grpcHost api.pubsub.salesforce.com and grpcPort 7443
2024-06-03 09:14:10,487 [main] java.lang.Class - Starting managed subscription with developer name My_Managed_Subscription
2024-06-03 09:14:10,663 [grpc-nio-worker-ELG-1-2] utility.APISessionCredentials -
Client Trace Id for current request: 22fdf915-3846-49b6-9943-09aa95b50903
2024-06-03 09:14:11,452 [grpc-default-executor-1] java.lang.Class - ManagedFetchResponse batch
of 5 events pending requested: 0
2024-06-03 09:14:11,453 [grpc-default-executor-1] java.lang.Class - RPC ID: 579112d8-c727-b880-94a1-1eceb6f72882
2024-06-03 09:14:11,453 [grpc-default-executor-1] java.lang.Class - processEvent -
EventID: 160810f4-f73f-4071-ad7f-a869b3e46dd8 SchemaId: _CADE3qaegX3ECEW0rlbYA
2024-06-03 09:14:11,461 [grpc-default-executor-1] utility.APISessionCredentials -
Client Trace Id for current request: e734fa7b-dd28-4f80-bfc8-94c06df82f25
2024-06-03 09:14:11,576 [grpc-default-executor-1] java.lang.Class - Received event:
{
  "CreatedDate": 1717431244798,
  "CreatedById": "005SG000009B3S1YAK",
  "Order_Number__c": "104",
  "City__c": "Los Angeles",
  "Amount__c": 35.0
}
2024-06-03 09:14:11,576 [grpc-default-executor-1] java.lang.Class - processEvent -
EventID: b13516f1-7c40-4b7f-a57f-cdb2cc936579 SchemaId: _CADE3qaegX3ECEW0rlbYA
2024-06-03 09:14:11,576 [grpc-default-executor-1] java.lang.Class - Received event:
{
  "CreatedDate": 1717431244798,
  "CreatedById": "005SG000009B3S1YAK",
  "Order_Number__c": "105",
  "City__c": "New York",
  "Amount__c": 20.0
}
2024-06-03 09:14:11,576 [grpc-default-executor-1] java.lang.Class - processEvent -
EventID: 24a4be7c-441c-4c42-886d-09e5de87c0c3 SchemaId: _CADE3qaegX3ECEW0rlbYA
2024-06-03 09:14:11,576 [grpc-default-executor-1] java.lang.Class - Received event:
{
  "CreatedDate": 1717431244798,
  "CreatedById": "005SG000009B3S1YAK",
  "Order_Number__c": "106",
  "City__c": "San Francisco",
  "Amount__c": 2.0
}
2024-06-03 09:14:11,576 [grpc-default-executor-1] java.lang.Class - processEvent -
EventID: 302f4a46-f970-4341-a1c5-63d36070cd36 SchemaId: _CADE3qaegX3ECEW0rlbYA
2024-06-03 09:14:11,576 [grpc-default-executor-1] java.lang.Class - Received event:
{
  "CreatedDate": 1717431244798,
  "CreatedById": "005SG000009B3S1YAK",
  "Order_Number__c": "107",
  "City__c": "San Jose",
  "Amount__c": 123.0
}
2024-06-03 09:14:11,576 [grpc-default-executor-1] java.lang.Class - processEvent -
EventID: a5d081d5-5678-4fdc-b3c9-3a52b61fc04a SchemaId: _CADE3qaegX3ECEW0rlbYA
2024-06-03 09:14:11,576 [grpc-default-executor-1] java.lang.Class - Received event:
{
  "CreatedDate": 1717431244798,
  "CreatedById": "005SG000009B3S1YAK",
  "Order_Number__c": "108",
  "City__c": "Boston",
  "Amount__c": 180.0
}
2024-06-03 09:14:11,576 [grpc-default-executor-1] java.lang.Class - Processed batch of 5 event(s)
2024-06-03 09:14:11,577 [grpc-default-executor-1] java.lang.Class - Sending CommitRequest with
CommitReplayRequest ID: 7cf61ac5-cb17-49ae-975e-4f236f194945
2024-06-03 09:14:11,577 [grpc-default-executor-1] java.lang.Class - Fetching more events: 5
2024-06-03 09:14:11,578 [main] java.lang.Class - Subscription Active. Received a total of 5 events.
2024-06-03 09:14:11,638 [grpc-default-executor-1] java.lang.Class - ManagedFetchResponse batch
of 0 events pending requested: 5
2024-06-03 09:14:11,638 [grpc-default-executor-1] java.lang.Class - RPC ID: 579112d8-c727-b880-94a1-1eceb6f72882
2024-06-03 09:14:11,638 [grpc-default-executor-1] java.lang.Class - Successfully committed replay
with CommitRequestId: 7cf61ac5-cb17-49ae-975e-4f236f194945 with process time: 1717431251600
2024-06-03 09:14:11,638 [main] java.lang.Class - Subscription Active. Received a total of 5 events.
```

Thanks to managed subscriptions (beta), you can track the events processed in your subscriptions. You don’t worry about how to catch up on missed events after a subscriber client is disconnected.
