Interface SessionController

  • All Known Implementing Classes:
    SessionControllerAdapter

    public interface SessionController
    Controls states and behaviour of one or multiple JDA instances.
    One instance of this should be used when sharding a bot account in order to keep track of session information between shards.

    The SessionControllerAdapter provides a default implementation that can be extended and overridden.

    States & Behaviour

    Identify Ratelimit Handling
    This will enable handling of (re-)connecting gateway sessions.

    Global REST Ratelimit
    The global REST ratelimit is not bound to a single session and should be handled on all JDA instances. This controller will receive updates of this ratelimit through setGlobalRatelimit(long) and should report the last ratelimit information it received through getGlobalRatelimit().

    Gateway Provider
    This provider can be used to change the gateway retrieval (using cache, http, or static) and allows to set a custom gateway endpoint. Use carefully.

    Examples

    Using JDABuilder
    
     JDABuilder builder = new JDABuilder(AccountType.BOT).setToken(BOT_TOKEN);
     builder.setSessionController(new SessionControllerAdapter() {
         @Override
         public void appendSession(SessionConnectNode node) {
             System.out.println("[SessionController] Adding SessionConnectNode to Queue!");
             super.appendSession(node);
         }
     });
     builder.addEventListeners(myListener);
     for (int i = 0; i < 10; i++) {
         builder.useSharding(i, 10).buildAsync();
     }
     

    Using ShardManager

    
     DefaultShardManagerBuilder builder = new DefaultShardManagerBuilder();
     builder.setToken(BOT_TOKEN);
     builder.setSessionController(new SessionControllerAdapter() {
         @Override
         public Pair<String, Integer> getGatewayBot(JDA api) {
             return Pair.of(getGateway(), 10);
         }
     });
     builder.addEventListeners(myListener);
     builder.build();
     
    • Method Detail

      • getGlobalRatelimit

        long getGlobalRatelimit()
        Provides the cross-session global REST ratelimit it received through setGlobalRatelimit(long).
        Returns:
        The current global REST ratelimit or -1 if unset
      • setGlobalRatelimit

        void setGlobalRatelimit​(long ratelimit)
        Called by the RateLimiter if the global rest ratelimit has changed.
        Parameters:
        ratelimit - The new global ratelimit
      • getGateway

        @Nonnull
        String getGateway​(@Nonnull
                          JDA api)
        Called by WebSocketClient when a new session starts (Connecting, Reconnecting).
        Should provide the gateway endpoint (wss) to connect to.
        Parameters:
        api - The current JDA instance (used for RestActions and ShardInfo)
        Returns:
        The gateway endpoint
      • getGatewayBot

        @Nonnull
        net.dv8tion.jda.internal.utils.tuple.Pair<String,​Integer> getGatewayBot​(@Nonnull
                                                                                      JDA api)
        Called by DefaultShardManager when a new shards is starting.
        Should provide a Pair with (gateway, shardTotal).
        Parameters:
        api - The current JDA instance (used for RestActions and ShardInfo)
        Returns:
        The Pair consisting of the gateway endpoint to connect to and the shardTotal
        See Also:
        getGateway(net.dv8tion.jda.api.JDA)