我面临一个问题,即我的简单 AGI 应用程序在保存/更新实体时挂起并且没有引发错误。从存储库中选择查询/方法按预期工作,没有任何错误。我正在将 Spring Boot 与 Spring Data JPA 一起使用,该 JPA 持久化到 MYSQL 数据库。
订阅者.java:
package com.newcom.ncivr.model;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import javax.persistence.*;
@Entity @Table(name = "subscribers")
public class Subscriber{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int Id;
private String msisdn;
private String subscriptionName;
@Column(columnDefinition = "DATETIME")
private DateTime lastRenewal;
private String status;
public Subscriber() {
}
public Subscriber(String msisdn, String subscriptionName) {
this.msisdn = msisdn;
this.subscriptionName = subscriptionName;
this.lastRenewal = DateTime.now(DateTimeZone.UTC).minusDays(1);
this.status = "ACTIVE";
}
//POJO Getters and Setters Excluded from Sample
}
订阅者存储库.java:
package com.newcom.ncivr.repository;
import com.newcom.ncivr.model.Subscriber;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface SubscriberRepository extends CrudRepository<Subscriber,Integer>{
Subscriber findFirstByMsisdnAndSubscriptionNameAndStatus(String msisdn, String subscriptionName, String status);
}
IvrAgi.java:
package com.newcom.ncivr.agi;
import com.newcom.ncivr.model.Subscriber;
import com.newcom.ncivr.repository.SubscriberRepository;
import org.asteriskjava.fastagi.*;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
public class IvrAgi extends BaseAgiScript {
private Logger log = LoggerFactory.getLogger(IvrAgi.class);
@Autowired
SubscriberRepository subscriberRepository;
public void service(AgiRequest request, AgiChannel channel) throws AgiException {
log.info("********** Agi Script Started. **************");
// Answer the channel...
answer();
log.info("Caller ID: "+request.getCallerIdNumber());
mainMenu(request);
log.info("********** Agi Script Completed. **************");
}
public void mainMenu(AgiRequest request) throws AgiException {
try {
while (true) {
String data = getData("ivr_main_menu");
log.info("User Input : " + data);
if (data.equals("1")) {
System.out.println(request.getCallerIdNumber() + ": User Selected 1");
streamFile("ivr_opt_one");
log.info(request.getCallerIdNumber() + ": User Selected 1");
try {
Subscriber subscriber = subscriberRepository.findFirstByMsisdnAndSubscriptionNameAndStatus(request.getCallerIdNumber(),"NEWS", "ACTIVE");
if(subscriber != null){
log.info("Id --->" + subscriber.toString());
subscriber.setLastRenewal(DateTime.now(DateTimeZone.UTC));
subscriber = subscriberRepository.save(subscriber);
log.info("Updated Entity --> " + subscriber.toString());
}else{
log.info("User is NOT Subscribed to NEWS");
}
}catch (Exception e){
e.printStackTrace();
}
hangup();
}else{
continue;
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
应用主要:
package com.newcom.ncivr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("classpath:/asterisk/asterisk-config.xml")
public class IvrApplication {
@Autowired
public static void main(String[] args) {
SpringApplication.run(IvrApplication.class, args);
}
}
asterisk-config.xml(将 Asterisk AGI 映射到 Spring Boot 服务):
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="agiServer" class="org.asteriskjava.fastagi.DefaultAgiServer"
init-method="startup" destroy-method="shutdown">
<!--<property name="port" value="1111"/>-->
<property name="mappingStrategy" ref="mappingStrategy"/>
</bean>
<bean id="vanivr" class="com.newcom.ncivr.agi.IvrAgi">
</bean>
<bean id="mappingStrategy" class="org.asteriskjava.fastagi.SimpleMappingStrategy">
<property name="mappings">
<map>
<entry key="vanivr.agi" value-ref="vanivr"/>
</map>
</property>
</bean>
</beans>
应用程序属性:
#Database connection settings
spring.datasource.url=jdbc:mysql://localhost:3306/ncivr?useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.jadira.usertype.autoRegisterUserTypes = true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.type=trace
logging.level.org.hibernate=TRACE
debug=true