Last active
February 29, 2016 06:21
-
-
Save jiang7462582/f80ff42f4315d9dc899b to your computer and use it in GitHub Desktop.
Activemq DI in Play 2.4x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
/** | |
* Mq Utills,Mq crete session and receive data function | |
*/ | |
import javax.jms.Message | |
import javax.jms.MessageListener | |
import javax.jms.Session | |
import javax.jms.TextMessage | |
import javax.inject.{Inject, Singleton} | |
import scala.language.postfixOps | |
import scala.collection.mutable.ListBuffer | |
import play.api.Configuration | |
import org.apache.activemq.ActiveMQConnectionFactory | |
import org.apache.activemq.pool.PooledConnectionFactory | |
@Singleton | |
class Mq @ Inject()(configuration: Configuration) { | |
val activeMQServerIPAddr = configuration.getString("activemq.server.ip").getOrElse("") | |
val activeMQServerUser = configuration.getString("activemq.server.user").getOrElse("") | |
val activeMQServerPasswd = configuration.getString("activemq.server.passwd").getOrElse("") | |
val factory: ActiveMQConnectionFactory = new ActiveMQConnectionFactory(activeMQServerIPAddr) | |
val pooledMaxConnectionSize: Int = 20 | |
val pooledFactory: PooledConnectionFactory = new PooledConnectionFactory(factory) | |
pooledFactory.setMaxConnections(pooledMaxConnectionSize) | |
val queueName = "helloPlay2.4" | |
def send(textMsg: String) = { | |
val connection = pooledFactory.createConnection(activeMQServerUser, activeMQServerPasswd) | |
val session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE) | |
val destination = session.createQueue(queueName) | |
val producer = session.createProducer(destination) | |
producer.send(session.createTextMessage(textMsg)) | |
session.close() | |
} | |
def recv: Session = { | |
val cf = new ActiveMQConnectionFactory(activeMQServerIPAddr) | |
cf.setCopyMessageOnSend(false) | |
val connection = cf.createConnection() | |
connection.start() | |
val session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE) | |
val destination = session.createQueue(queueName) | |
val consumer = session.createConsumer(destination) | |
val listener = new MessageListener { | |
def onMessage(message: Message) { | |
message match { | |
case text: TextMessage => { | |
// consumer message via ws | |
val textContentComp = text.getText | |
// decompress the text msg received | |
println(textContentComp) | |
} | |
case _ => { | |
throw new Exception("Unhandled message type: " + message.getClass.getSimpleName) | |
} | |
} | |
} | |
} | |
consumer.setMessageListener(listener) | |
session | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GlobalApp @ Inject()(mq:Mq) { | |
val consumers = StartApp.consumers | |
consumers += mq.recv | |
} | |
object GlobalApp{ | |
lazy val consumers = new ListBuffer[Session]() | |
def stop() = { // close all mq session | |
consumers.toList.foreach((session: Session) => { | |
session.close() | |
}) | |
} | |
} | |
import javax.inject.{Inject, Singleton} | |
import play.api.inject.ApplicationLifecycle | |
import scala.concurrent.Future | |
@Singleton | |
class AppStop @Inject()(lifecycle: ApplicationLifecycle,mq:Mq) { | |
lifecycle.addStopHook(() => Future.successful(GlobalApp.stop())) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import com.google.inject.AbstractModule | |
/** | |
* Created by jiang | |
*/ | |
class onStartModule extends AbstractModule{ | |
override def configure() = { | |
bind(classOf[GlobalApp]).asEagerSingleton() | |
bind(classOf[AppStop]).asEagerSingleton() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
play.modules.enabled += "models.onStartModule" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment