I am writing a simple POC using Apache Camel to receive SMNP traps. I am trying to take the incoming exchange message, convert it to SnmpMessage
and then create a custom type that holds two fields from the SnmpMessage
. I then want to save this custom object to an H2 DB. My SNMP route looks like this:
open class SnmpTrapRoute(private val repository: IPduEventRepository) : RouteBuilder() {
@Throws(Exception::class)
override fun configure() {
from("snmp:0.0.0.0:1611?protocol=udp&type=TRAP")
.process { exchange ->
val message = exchange.getIn() as SnmpMessage
val pduEvent = CustomPduEvent(message.snmpMessage.requestID.toLong(), message.snmpMessage.type)
exchange.getIn().setBody(pduEvent, CustomPduEvent::class.java)
}
.bean(repository, "save")
.log("MIH :: DB Entries [" + repository.findAll() + "]")
}
}
However, the .bean
call doesn't seem to add the object to the DB. If I include repository.save(pduEvent)
in the processor and remove the call to .bean
, it adds it fine.
Have also tried setting the out message body exchange.out.setBody(pduEvent, PduEvent::class.java)
but same result.
Where am I going wrong?
It turned out I had two main problems. Firstly, I needed to set the body of the out message, not the in, and secondly, the final .log
call didn't work when calling a bean method. So the call to the bean method was working, but the log call was making it look like it didn't. So instead I called my logger from within a process
call.
from("snmp:0.0.0.0:1611?protocol=udp&type=TRAP")
.process { exchange ->
val message = exchange.getIn() as SnmpMessage
exchange.out.body = CustomPduEvent(message.snmpMessage.requestID.toLong(), message.snmpMessage.type)
}
.bean(repository, "save")
.process {
logger.debug("MIH :: DB Entries [" + repository.findAll() + "]")
}