@@ -56,20 +56,16 @@ public abstract class LongPollBot extends VkBot {
5656 * @throws VkApiException if errors occur.
5757 */
5858 public void startPolling () throws VkApiException {
59- initialize ();
6059 while (polling ) {
6160 try {
62- if (isSessionExpired ()) {
63- initialize ();
64- }
6561 GetUpdates .ResponseBody updates = getUpdates ().execute ();
66- ts = updates .getTs ();
62+ setTs ( updates .getTs () );
6763 handle (updates .getEvents ());
6864 } catch (VkResponseException e ) {
6965 if (!e .getMessage ().contains ("failed" )) {
7066 throw e ;
7167 }
72- initialize () ;
68+ initializedAt = null ;
7369 }
7470 }
7571 }
@@ -95,10 +91,10 @@ public void initialize() throws VkApiException {
9591 .getResponse ()
9692 .getAsJsonObject ();
9793
98- server = longPollServer .get ("server" ).getAsString ();
99- key = longPollServer .get ("key" ).getAsString ();
100- if (ts == null ) {
101- ts = longPollServer .get ("ts" ).getAsInt ();
94+ setServer ( longPollServer .get ("server" ).getAsString () );
95+ setKey ( longPollServer .get ("key" ).getAsString () );
96+ if (getTs () == null ) {
97+ setTs ( longPollServer .get ("ts" ).getAsInt () );
10298 }
10399 }
104100
@@ -125,9 +121,36 @@ private boolean isSessionExpired() {
125121 *
126122 * @return {@link GetUpdates} instance.
127123 */
128- private GetUpdates getUpdates () {
129- return new GetUpdates (server )
130- .setKey (key )
131- .setTs (ts );
124+ public GetUpdates getUpdates () throws VkApiException {
125+ if (initializedAt == null || isSessionExpired ()) {
126+ initialize ();
127+ }
128+ return new GetUpdates (getServer ())
129+ .setKey (getKey ())
130+ .setTs (getTs ());
131+ }
132+
133+ public String getServer () {
134+ return server ;
135+ }
136+
137+ public void setServer (String server ) {
138+ this .server = server ;
139+ }
140+
141+ public String getKey () {
142+ return key ;
143+ }
144+
145+ public void setKey (String key ) {
146+ this .key = key ;
147+ }
148+
149+ public Integer getTs () {
150+ return ts ;
151+ }
152+
153+ public void setTs (Integer ts ) {
154+ this .ts = ts ;
132155 }
133156}
0 commit comments