Nespony
Nespony

Reputation: 1371

using package in Scala?

I have a scala project that uses akka. I want the execution context to be available throughout the project. So I've created a package object like this:

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import com.datastax.driver.core.Cluster

package object connector {

  implicit val system = ActorSystem()
  implicit val mat = ActorMaterializer()

  implicit val executionContext = executionContext

    implicit val session = Cluster
    .builder
      .addContactPoints("localhost")
      .withPort(9042)
      .build()
      .connect()


}

In the same package I have this file:

import akka.stream.alpakka.cassandra.scaladsl.CassandraSource
import akka.stream.scaladsl.Sink
import com.datastax.driver.core.{Row, Session, SimpleStatement}

import scala.collection.immutable
import scala.concurrent.Future

object CassandraService {

    def selectFromCassandra()() = {

      val statement = new SimpleStatement(s"SELECT * FROM animals.alpakka").setFetchSize(20)
      val rows: Future[immutable.Seq[Row]] = CassandraSource(statement).runWith(Sink.seq)

      rows.map{item =>
       print(item)

      }
    }
}

However I am getting the compiler error that no execution context or session can be found. My understanding of the package keyword was that everything in that object will be available throughout the package. But that does not seem work. Grateful if this could be explained to me!

Upvotes: 0

Views: 89

Answers (2)

KZapagol
KZapagol

Reputation: 928

You have two issue with your current code.

  1. When you compile your package object connector it is throwing below error

    Error:(14, 35) recursive value executionContext needs type implicit val executionContext = executionContext Issue is with implicit val executionContext = executionContext line Solution for this issue would be as below.

    implicit val executionContext = ExecutionContext

  2. When we compile CassandraService then it is throwing error as below

    Error:(17, 13) Cannot find an implicit ExecutionContext. You might pass an (implicit ec: ExecutionContext) parameter to your method or import scala.concurrent.ExecutionContext.Implicits.global. rows.map{item =>

Error clearly say that either we need to pass ExecutionContext as implicit parameter or import scala.concurrent.ExecutionContext.Implicits.global. In my system both issues are resolved and its compiled successfully. I have attached code for your reference.

package com.apache.scala

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import com.datastax.driver.core.Cluster
import scala.concurrent.ExecutionContext


package object connector {

  implicit val system = ActorSystem()
  implicit val mat = ActorMaterializer()

  implicit val executionContext = ExecutionContext

  implicit val session = Cluster
    .builder
    .addContactPoints("localhost")
    .withPort(9042)
    .build()
    .connect()

}

package com.apache.scala.connector

import akka.stream.alpakka.cassandra.scaladsl.CassandraSource
import akka.stream.scaladsl.Sink
import com.datastax.driver.core.{Row, SimpleStatement}

import scala.collection.immutable
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

object CassandraService {

  def selectFromCassandra() = {

    val statement = new SimpleStatement(s"SELECT * FROM animals.alpakka").setFetchSize(20)
    val rows: Future[immutable.Seq[Row]] = CassandraSource(statement).runWith(Sink.seq)

    rows.map{item =>
      print(item)

    }
  }

}

Upvotes: 1

Rex
Rex

Reputation: 568

Your implementation must be something like this, and hope it helps.

package.scala

package com.app.akka

package object connector {
  // Do some codes here..
}

CassandraService.scala

package com.app.akka
import com.app.akka.connector._

object CassandraService {
  def selectFromCassandra() = {
    // Do some codes here..
  }
}

Upvotes: 1

Related Questions