How to Receive a Message from an Apache ActiveMQ Topic with Frege

W/o Durability

package ActiveMQSample where

data ActiveMQConnectionFactory = native org.apache.activemq.ActiveMQConnectionFactory where
  native createConnection :: Mutable s ActiveMQConnectionFactory -> STMutable s Connection throws JMSException
  native new :: () -> STMutable s ActiveMQConnectionFactory

data Connection = native javax.jms.Connection where
  native close :: Mutable s Connection -> ST s () throws JMSException
  native createSession :: Mutable s Connection -> Bool -> Int -> STMutable s Session throws JMSException
  native start :: Mutable s Connection -> ST s () throws JMSException

data Destination = pure native javax.jms.Destination

data JMSException = pure native javax.jms.JMSException
derive Exceptional JMSException

data Message = pure native javax.jms.Message

data MessageConsumer = native javax.jms.MessageConsumer where
  native receive :: Mutable s MessageConsumer -> ST s Message throws JMSException

data Session = mutable native javax.jms.Session where
  native autoAcknowledge javax.jms.Session.AUTO_ACKNOWLEDGE :: Int
  native createConsumer :: Mutable s Session -> Destination -> STMutable s MessageConsumer throws JMSException
  native createTopic :: Mutable s Session -> String -> ST s Topic throws JMSException

data Topic = pure native javax.jms.Topic

instance Show Message where
  pure native show toString :: Message -> String

main _ = do
  connFactory <- ActiveMQConnectionFactory.new ()

  conn <- connFactory.createConnection

  sess <- conn.createSession false Session.autoAcknowledge

  dest <- sess.createTopic "SampleTopic"

  cons <- sess.createConsumer dest

  conn.start

  msg <- cons.receive

  println msg

  conn.close

W/ Durability

package ActiveMQSample where

data ActiveMQConnectionFactory = native org.apache.activemq.ActiveMQConnectionFactory where
  native createConnection :: Mutable s ActiveMQConnectionFactory -> STMutable s Connection throws JMSException
  native new :: () -> STMutable s ActiveMQConnectionFactory

data Connection = native javax.jms.Connection where
  native close :: Mutable s Connection -> ST s () throws JMSException
  native createSession :: Mutable s Connection -> Bool -> Int -> STMutable s Session throws JMSException
  native setClientID :: Mutable s Connection -> String -> ST s () throws JMSException
  native start :: Mutable s Connection -> ST s () throws JMSException

data JMSException = pure native javax.jms.JMSException
derive Exceptional JMSException

data Message = pure native javax.jms.Message

data MessageConsumer = native javax.jms.MessageConsumer where
  native receive :: Mutable s MessageConsumer -> ST s Message throws JMSException

data Session = mutable native javax.jms.Session where
  native autoAcknowledge javax.jms.Session.AUTO_ACKNOWLEDGE :: Int
  native createDurableSubscriber :: Mutable s Session -> Topic -> String -> STMutable s MessageConsumer throws JMSException
  native createTopic :: Mutable s Session -> String -> ST s Topic throws JMSException

data Topic = pure native javax.jms.Topic

instance Show Message where
  pure native show toString :: Message -> String

main _ = do
  connFactory <- ActiveMQConnectionFactory.new ()

  conn <- connFactory.createConnection

  conn.setClientID "SampleClient"

  sess <- conn.createSession false Session.autoAcknowledge

  dest <- sess.createTopic "SampleTopic"

  cons <- sess.createDurableSubscriber dest "SampleSubscription"

  conn.start

  msg <- cons.receive

  println msg

  conn.close
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.