I implemented "Click to Call" using Aloha and RabbitMQ, testing the solution on sipX.
I have a grails PlaceCallService that sets up a connection to our RabbitMQ instance like this:
static transactional = false;
ConnectionParameters connectionParameters;
ConnectionFactory connectionFactory;
ConfigObject config = ConfigurationHolder.config;
MessageQueueService() {
connectionParameters = new ConnectionParameters();
connectionParameters.setUsername(config.mq.username);
connectionParameters.setPassword(config.mq.password);
connectionFactory = new ConnectionFactory(connectionParameters);
}
The actual message publish function looks something like this:
def publish(message) {
try {
Connection conn = connectionFactory.newConnection(config.mq.host,
AMQP.PROTOCOL.PORT);
Channel ch = conn.createChannel();
ch.queueDeclare(config.placeCall.routingKey);
ch.basicPublish("", config.placeCall.routingKey, null,
message.getBytes());
ch.close();
conn.close();
}
catch (Exception e) {
log.error("Main thread caught exception: " + e);
return false
}
return true
}
Then in a "Third Party call initiator daemon", I unpack the message and use the Aloha stack to do a
try {
OutboundCallLegBean outboundCallLegBean = (OutboundCallLegBean)
applicationContext.getBean("outboundCallLegBean");
CallBean callBean = (CallBean)
applicationContext.getBean("callBean");
callBean.addCallListener(this);
// create two call legs
String callLegId1 =
outboundCallLegBean.createCallLeg(URI.create(callee),
URI.create(caller));
String callLegId2 =
outboundCallLegBean.createCallLeg(URI.create(caller),
URI.create(callee));
// join the call legs
System.out.println(String.format("connecting %s and %s in call...$
System.out.println(callBean.joinCallLegs(callLegId1, callLegId2))$
}
This chunk of code is based on the helpful Third Party Call sample from Aloha's subversion repository.
Anyway, it's working well, consumes minimal resources on the web server (just post message to the "/placeCall/request" queue), and only took a few days to setup and deploy into production. Many thanks to the Aloha team, RabbitMQ folks, and sipX gurus.
No comments:
Post a Comment