我有一个api,它接收来自一个源的数据,并将它们发送给电报机器人。
我从我的源接收大量数据,并将它们以这样的速率发送给电报bot,但是电报每秒只能处理一条消息,所以它最终会返回这个异常。
java.io.IOException: Server returned HTTP response code: 429 for URL:....是否有方法将消息存储在列表中并从线程中迭代此列表?
我正在努力学习java,所以如果我的代码不好,请不要介意。
Sample.java
class Sample{
run(){
while(true){
//some operations
SendMessage.getInstance().sendToTelegram(clientCommand);
//
}
}
}SendMessage.java
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class SendMessage {
static SendMessage getInstance() {
return instance;
}
public void sendToTelegram(String message) {
String urlString = "https://api.telegram.org/;
String apiToken = obj.getInstance().getTelegramToken();
String chatId = obj.getInstance().getChatId();
String text = message;
urlString = urlString+"/bot"+apiToken+"/sendMessage?parse_mode=HTML&chat_id="+chatId+"&text="+msgToSend;
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
InputStream is = new BufferedInputStream(conn.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String inputLine = "";
StringBuilder sb = new StringBuilder();
while ((inputLine = br.readLine()) != null) {
sb.append(inputLine);
sb.append('\r');
}
br.close();
} catch (IOException e) {
log.error(e);
}
}
}如果线程概念有效,请任何人帮助我添加到列表中,并将它们发送到电报bot中,而不丢失数据。
使用休眠线程不会得到429个太多的响应异常。
class Sample{
run(){
while(true){
//some operations
SendMessage.getInstance().sendToTelegram(clientCommand);
Thread.sleep(2000);
}
}
}但是得到新的异常错误请求
java.io.IOException: Server returned HTTP response code: 400 for URL这是演示电报url
https://api.telegram.org/botid:TELEGRAM_TOKEN/sendMessage?parse_mode=HTML&chat_id=CHAT_ID&text=<b>Alert</b>%0A<b>Alert Name:</b> "REGISTER Violation"%0A<b>Severity:</b> "Medium"%0A<b>TimeStamp:</b> "2022-05-10 22:17:34.31"%0A<b>Event ID:</b> "160"%0A<b>Event Message:</b> "An unregistered User has been detected. This can be a Caller-ID poisoning or Number Harvesting attack. Only a valid registered user can make or receive calls"%0A<b>Source Contact:</b> "192.168.3.31:5077"%0A<b>Destination Contact:</b> "192.168.10.10:5555"%0A<b>Source IP:</b> "192.168.3.31"%0A<b>Destination IP:</b> "192.168.10.10"%0A<b>Source Ext:</b> "4545454545"%0A<b>Destination Ext:</b> "%2B43965272"%0A<b>Source Domain:</b> "n/a"%0A<b>Destination Domain:</b> "n/a"%0A<b>Protocol:</b> "SIP"%0A<b>Comment:</b> "None"%0A<b>Attack Name:</b> "REGISTER Violation"%0A<b>Method:</b> "INVITE"%0A<b>Source Country:</b> "Unknown"%0A<b>Destination Country:</b> "AUSTRIA"%0A<b>CallType:</b> "International"%0A<b>RiskScore:</b> "0"%0A<b>Client Name:</b> "Unknown:Unknown"%0A<b>Network Group Name:</b> "defaultNonVlanGroup"%0A<b>Acknowledged:</b> "No"%0A<b>Alert Category:</b> "External"%0A<b>UCTM Name:</b> "redshift" 我手动尝试粘贴url,它显示在异常中,但它工作得很好,但在应用程序中,它抛出了这个异常。
请帮我做错事
发布于 2022-05-09 08:14:47
您可以在循环中执行一个简单的Thread.sleep(2000)。可能规模不太好
或者,您可以将所有消息存储在同步列表(https://www.techiedelight.com/queue-implementation-in-java/)中,并创建一个调度程序,每隔x秒读取一条消息,发送消息并将其从列表中删除。如果您使用Spring,这是相当容易的-> https://www.baeldung.com/spring-task-scheduler
https://stackoverflow.com/questions/72167138
复制相似问题