BrightSide Workbench Full Report + Source Code
PushPoolSender.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.push.sender;
20 
21 import java.util.function.Consumer;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.assistant.Assistant;
25 import org.turro.assistant.AssistantSet;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.push.UserPushSubscription;
28 
33 public class PushPoolSender implements Runnable {
34 
35  private final AssistantSet assistants;
36  private final String payload;
37  private Consumer onFinish;
38 
39  public PushPoolSender(AssistantSet assistants, String payload) {
40  this.assistants = assistants;
41  this.payload = payload;
42  }
43 
44  public void send(Consumer onFinish) {
45  this.onFinish = onFinish;
46  if(assistants.size() > 20) {
47  new Thread(this).start();
48  } else {
49  run();
50  }
51  }
52 
53  @Override
54  public void run() {
55  //int i = 1, count = assistants.size();
56  for (Assistant assistant : assistants) {
57  if(assistant.hasContact()) UserPushSubscription.pushMessage(assistant.contact.getId(), payload);
58  Logger.getLogger(PushPoolSender.class.getName()).log(Level.INFO, "Pushed message to {0}. Payload: {1}",
59  new Object[]{assistant.name, payload});
60  //if(count > 20 && ++i < count) Thread.sleep(100); // sleep only when using thread
61  }
62  if(onFinish != null) {
63  try {
64  onFinish.accept(null);
65  } catch (Exception ex) {
66  Logger.getLogger(PushPoolSender.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
67  }
68  }
69  }
70 
71 }
static void pushMessage(String idContact, String payload)
PushPoolSender(AssistantSet assistants, String payload)