Quantcast
Channel: Akka Libraries - Discussion Forum for Akka technologies
Viewing all articles
Browse latest Browse all 1362

Unexpected behavior when connecting JMS consumer source to JMS producer sink

$
0
0

Hi,

I’m new to Alpakka (and JMS). I’m attaching a minimal example where I publish messages to topic1 using a tick source (1 message per second) and a JmsProducer sink. I then subscribe to topic1 using a JmsConsumer source and print out the messages as they arrive. This works as expected, i.e., I see one message per second printed.

I then connect the consumer source for topic1 to a producer sink for topic2. I would expect to see messages arriving in topic2 at the same rate as for topic1. Instead, no messages are published to topic2 at all and instead the source for topic1 seems to get short-circuited and produces messages at a rate of 100s per second. Adding a throttle between topic1 consumer and topic2 producer reduces the rate to 1/second again for topic1, but still no messages arrive in topic2.

What am I missing here?

package com.example

import akka.actor.typed.ActorSystem
import akka.actor.typed.scaladsl.Behaviors

import akka.stream.scaladsl.Source;

import akka.stream.alpakka.jms.JmsConsumerSettings;
import akka.stream.alpakka.jms.JmsProducerSettings;
import akka.stream.alpakka.jms.JmsTextMessage
import akka.stream.alpakka.jms.scaladsl.JmsConsumer;
import akka.stream.alpakka.jms.scaladsl.JmsProducer;

import scala.concurrent.duration._

import javax.jms.TextMessage

object Main extends App {

  implicit val system = ActorSystem(Behaviors.empty, "AkkaQuickStart")
  implicit val ec = system.executionContext

  val url =
    "tcp://localhost:61616" // running ActiveMQ broker in Docker container

  val connectionFactory: javax.jms.ConnectionFactory =
    new org.apache.activemq.ActiveMQConnectionFactory(url)

  val producerSink = JmsProducer.sink(
    JmsProducerSettings(system, connectionFactory).withTopic("topic1")
  )

  // this works - publishes 1 message / second to `topic1`
  Source
    .tick(0.seconds, 1.second, "hi")
    .map(JmsTextMessage(_))
    .runWith(producerSink)

  val consumerSource = JmsConsumer(
    JmsConsumerSettings(system, connectionFactory).withTopic("topic1")
  ).collect { case message: TextMessage =>
    JmsTextMessage(message)
  }

  // this also works - consumes messages as they arrive in `topic1`
  consumerSource.runForeach(t =>
    println(s"consumer of topic1: ${t} - ${t.body}")
  )

  val producerSink2 = JmsProducer.sink(
    JmsProducerSettings(system, connectionFactory).withTopic("topic2")
  )

  // this doesn't work - doesn't publish to `topic2`; instead short-circuits `topic1` producer to produce 100s of messages per second.
  consumerSource.runWith(producerSink2)

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 1362

Trending Articles