0

I have a flink application. I use an object inside the map function. Like this:

.map(value => {
    import spray.json._
    import com.webtrekk.sObjects._
    import com.webtrekk.jsonProtocol._
    import com.webtrekk.salesforce._

    implicit val effortProcessing = streamProcessor.Effort
    implicit val effortConsulting = effortConsultingFormat
    var effort = value.toString.parseJson.convertTo[effortConsulting]

    streamProcessor.Effort.mapping(
        value.toString.parseJson.convertTo[effortConsulting]
    )

    effort
  })

The streamProcessor is a object. Inside this object is another service object for the database. Flink executes this map function every time when an event comes to the application. What i want to know: Is the object every time the identical singleton object?

An example:

-> event comes to the application -> map function will execute and a singleton object will created

-> next event comes to the application -> map function will execute again -> object will called again

Is the second object the identical instance?

Citrullin
  • 2,269
  • 14
  • 29
  • 1
    For each Scala `object` there is only one instance, per JVM (which may so not be the case in case of distributed computing ...) – cchantep May 20 '16 at 14:07
  • Yes, i mean inside one JVM. If i create a new JVM instance, it's a new object. – Citrullin May 20 '16 at 14:13

1 Answers1

2

Yes and no. An object is a singleton within its scope:

scala> List(1, 2).map(i => { object foo { def bar: Int = i }; foo }).map(_.bar)
res2: List[Int] = List(1, 2)

This means that the following are loosely equivalent:

object foo extends Thing { ... }
lazy val foo = new Thing { ... }

In your case because the object is declared outside of the map function, it is the same instance every time.

J Cracknell
  • 3,498
  • 1
  • 19
  • 13