package cn.sagacloud.server.algorithm.test

import cn.sagacloud.server.algorithm.services.activemq.JMSComsumer
import cn.sagacloud.server.algorithm.services.activemq.JMSProducer
import org.apache.activemq.ActiveMQConnectionFactory
import org.apache.activemq.command.ActiveMQTopic
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
import java.util.*
import javax.jms.DeliveryMode
import javax.jms.Session

/**
 *
 *
 * @author jxing
 */
@RunWith(SpringRunner::class)
@SpringBootTest
class JMSTest {

    @Autowired
    lateinit var jmsComsumer: JMSComsumer

    @Autowired
    lateinit var producer: JMSProducer

    @Test
    fun producer(){
        val conFactory = ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.20.225:61616")
        val conn = conFactory.createConnection() // 用户名和密码
        val session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE)

        val destination = session.createTopic("myprivate")

        val producer = session.createProducer(destination)

        producer.deliveryMode = DeliveryMode.NON_PERSISTENT

        conn.start()
        val msg = session.createTextMessage("hello, world ,Time : ${Date()}")
        producer.send(msg)

        producer.close()
        session.close()
        conn.close()
    }

    @Test
    fun consumer(){
        val consumer = JMSComsumer()
//        consumer.receiveMessage()
        while (true){
            Thread.sleep(1000)
        }
    }

    @Test
    fun newConsumer(){
//        jmsComsumer.receiveTopic()
    }

    @Test
    fun newProducer(){
        val destination = ActiveMQTopic("model.manage")
        producer.sendMessage(destination, "ggggg")
    }
}