(Yeah, I know. I have a lot of questions today.)
While I know that SMX 3 + JBI can be used to create scripting
endpoints to call out to various languages (JS, Ruby, Groovy), I'm
missing some essential documentation on how to create a bean in SMX 4
+ OSGi to run a script. In particular, I'm looking for the right bits
to import + use in my bean to call a Ruby script via JRuby.
Here's my bean code:
public class OutboundSMSRequestHandlerImpl implements OutboundSMSRequestHandler,
InitializingBean, DisposableBean
{
private static final Logger logger =
LoggerFactory.getLogger(OutboundSMSRequestHandlerImpl.class);
private ScriptEngineManager m_scriptManager = null;
private ScriptEngine m_rubyEngine = null;
private Invocable m_invocableEngine = null;
@Override
@Consume(uri="activemq:queue:outbound_sms_req")
public void handle(@Body OutboundSMSRequest outboundSMSRequest)
{
// do stuff
FileReader f = new FileReader("thisScript.rb");
m_rubyEngine.eval(f);
Object[] args = {"one", "two"};
Object obj =
m_invocableEngine.invokeFunction("RubyHandleInboundSMSFunc", args);
//Object obj =
m_invocableEngine.invokeFunction("RubyHandleInboundSMSFunc", "junk");
}
catch (ScriptException se)
{
/* error */
logger.error("ScriptException: " + se.getMessage());
}
catch (IOException ioe)
{
/* error */
logger.error("IOException: " + ioe.getMessage());
}
catch (NoSuchMethodException nsme)
{
/* error */
logger.error("NoSuchMethodException: " + nsme.getMessage());
}
catch (Exception e)
{
/* error */
logger.error("Exception: " + e.getMessage());
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace (pw);
logger.error(sw.toString());
}
...
}
@Override
public void afterPropertiesSet() throws Exception
{
logger.info("Starting Outbound SMS Request Handler 1");
m_scriptManager = new ScriptEngineManager();
List<ScriptEngineFactory> engines =
m_scriptManager.getEngineFactories();
for (ScriptEngineFactory engine : engines) {
logger.info("Engine name: " + engine.getEngineName());
}
m_rubyEngine = m_scriptManager.getEngineByName("jruby");
if (m_rubyEngine == null)
{
logger.info("there is no jruby engine");
}
else
{
m_invocableEngine = (Invocable) m_rubyEngine;
}
}
@Override
public void destroy() throws Exception
{
logger.info("Stopping Outbound SMS Request Handler");
}
}
And here's the section of my pom.xml that generates the bundle:
...
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.0</version>
<extensions>true</extensions>
<configuration>
<manifestLocation>META-INF</manifestLocation>
<instructions>
<Export-Package>
com.ateb.dataproc.obc_processor
</Export-Package>
<Private-Package>
com.ateb.dataproc.obc_processor.internal
</Private-Package>
<Import-Package>
org.apache.activemq,
org.apache.camel,
org.apache.camel.component.bean,
org.apache.camel.component.jms,
com.google.code.scriptengines.jruby,
*
</Import-Package>
<Include-Resource>
{maven-resources},
src/main/resources/dataspecs/
</Include-Resource>
<Require-Bundle>com.ateb.dataproc.obc-api,org.apache.camel.camel-jms,org.apache.servicemix.bundles.jruby,scriptengines-jruby</Require-Bundle>
</instructions>
</configuration>
</plugin>
...
I'm not sure if the problem is:
a) a lack of specified deps, or
b) creation afterPropertiesSet is too soon (probably not)
Any help on this? BTW, if I'm Doing It Wrong, and this is not the Best
Practice, let me know, too.
--sgp