Search code examples
ractivemq-classicmessage-queueamazon-sqs

Connecting to ActiveMQ (or SQS) on R


I can't find any code that works and isn't outdated for modern revisions of R. Some GitHub repository imports work (added below), but none are meant for ActiveMQ specifically.

remotes::install_github("cloudyr/aws.s3")
​remotes::install_github("Azure/AzureQstor")
​install.packages("rzmq")​
remotes::install_github("lecardozo/rworker")

I need it to work on R version 3.6.3. The closest I got to have it working is through Apache's referred code:

Sys.setenv(JAVA_HOME="C:/Program Files/AdoptOpenJDK/jre-8.0.242.08-hotspot")  #custom provision of Java SDK
library("rJava")
.jinit()
attach(javaImport(c("javax.jms", "org.apache.activemq")))
connFactory <- new(ActiveMQConnectionFactory)  # ERROR: R doesn't recognize "new"
conn <- connFactory$createConnection()
sess <- conn$createSession(FALSE, Session$AUTO_ACKNOWLEDGE)
dest <- sess$createQueue("SampleQueue")
prod <- sess$createProducer(dest)
msg <- sess$createTextMessage("Simples Assim")
prod$send(msg)
conn$close()

I could compromise for Amazon-SQS since I'm mainly restricted to Amazon MQ services. If there's no library for me to import, what alternative approach could I take?


Solution

  • Worked:

    library("Rjms")
    attach(javaImport(c("java.lang", "javax.jms", "org.apache.activemq")))
    connFactory <- new(ActiveMQConnectionFactory)
    conn <- connFactory$createConnection()
    sess <- conn$createSession(FALSE, Session$AUTO_ACKNOWLEDGE)
    dest <- sess$createQueue("SampleQueue")
    cons <- sess$createConsumer(dest)
    conn$start()
    msg <- cons$receive()
    System$out$println(msg)
    conn$close()