V2.2.2 Allow sending out messages periodically using external xml

master
Ng Yat Yan 1 month ago
parent 5b7b4f6a1b
commit 5084c50f1c

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task https://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<task:scheduler id="myScheduler" pool-size="10" />
<bean id="testMessageSender"
class="com.example.sshd.service.MessageSender">
<property name="fromJID" value="" />
<property name="toJID" value="" />
<property name="message" value="" />
</bean>
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="testMessageSender"
method="sendMessage" cron="* * * * * ?" />
</task:scheduled-tasks>
</beans>

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.sshd</groupId>
<artifactId>echo-sshd-server</artifactId>
<version>2.2.1</version>
<version>2.2.2</version>
<name>ECHO SSH SERVER</name>
<description>Learning Apache Mina SSHD library</description>
<parent>

@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Scope;
import org.xmpp.packet.Message;
@ -25,6 +26,7 @@ import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
@Configuration
@ImportResource("file:conf/scheduler.xml")
public class AppConfig {
@Value("${xmpp-component.config-json}")

@ -74,6 +74,20 @@ public class EchoComponent extends AbstractComponent {
return this.getClass().getName();
}
public void sendMessage(String fromJID, String toJID, String message) {
try {
Message outMsg = new Message();
outMsg.setType(Message.Type.chat);
outMsg.setFrom(fromJID);
outMsg.setTo(toJID);
outMsg.setBody(replyService.executeShellCommand(message));
externalComponentManager.sendPacket(this, outMsg);
logger.info("[sendMessage] -- SENT -- {}", outMsg);
} catch (Exception err) {
logger.error("[sendMessage] ", err);
}
}
private void doEcho(final Message inMsg, String body) {
try {
Message outMsg = new Message();

@ -0,0 +1,39 @@
package com.example.sshd.service;
import org.springframework.beans.factory.annotation.Autowired;
public class MessageSender {
@Autowired
EchoComponent echoComponent;
private String fromJID, toJID, message;
public void sendMessage() {
echoComponent.sendMessage(fromJID, toJID, message);
}
public String getFromJID() {
return fromJID;
}
public void setFromJID(String fromJID) {
this.fromJID = fromJID;
}
public String getToJID() {
return toJID;
}
public void setToJID(String toJID) {
this.toJID = toJID;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Loading…
Cancel
Save