参照asterisk-java提供的示例,用以下方法执行了CLI命令:
import java.io.IOException;
import java.util.List;
import org.asteriskjava.manager.AuthenticationFailedException;
import org.asteriskjava.manager.ManagerConnection;
import org.asteriskjava.manager.ManagerConnectionFactory;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.manager.action.CommandAction;
import org.asteriskjava.manager.response.CommandResponse;
public class HelloManager {
private ManagerConnection connection;
public HelloManager() throws IOException {
ManagerConnectionFactory factory = new ManagerConnectionFactory("10.10.1.101", "manager", "manager");
this.connection = factory.createManagerConnection();
}
public void run() throws IOException, AuthenticationFailedException, TimeoutException {
CommandAction action;
CommandResponse response;
action = new CommandAction();
action.setCommand("odbc show");
// connect to Asterisk and log in
connection.login();
// send the originate action and wait for a maximum of 30 seconds for Asterisk
// to send a reply
response = (CommandResponse) connection.sendAction(action, 30000);
// print out whether the originate succeeded or not
System.out.println(response.getResponse());
// result
List<String> result = response.getResult();
for (String str : result) {
System.out.println(str);
}
// and finally log off and disconnect
connection.logoff();
}
public static void main(String[] args) throws Exception {
HelloManager helloManager;
helloManager = new HelloManager();
helloManager.run();
}
}
asterisk-java提供了很多Action类用于执行不同的命令。
附件为asterisk-java的chm格式帮助文件。
本文介绍如何使用Java库asterisk-java来执行Asterisk的CLI命令。通过示例代码展示连接到Asterisk服务器并发送odbcshow命令的过程。包括建立连接、登录、发送命令及接收响应等步骤。
330

被折叠的 条评论
为什么被折叠?



