Как внедрить зависимость-CqlSession? - PullRequest
0 голосов
/ 03 мая 2020

Чтобы использовать ShedLock с Кассандрой, мне нужно CqlSession, чтобы создать CassandraLockProvider, в соответствии с документами .

Однако я всегда получаю следующее ошибка:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.datastax.oss.driver.api.core.CqlSession' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Этот полный минимальный пример ниже воспроизводит проблему:

src/main/kotlin/com/foo/cqlsessioninjection/application.kt

package com.foo.cqlsessioninjection


import com.datastax.oss.driver.api.core.CqlSession
import net.javacrumbs.shedlock.core.LockProvider
import net.javacrumbs.shedlock.provider.cassandra.CassandraLockProvider
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component


@SpringBootApplication
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "PT23H")
class CqlSessionInjection


@Configuration
class SchedulerConfiguration {
    @Bean
    fun lockProvider(cqlSession: CqlSession): LockProvider {
        return CassandraLockProvider(cqlSession)
    }
}

@Component
class SomeTask {
    @Scheduled(cron = "*/1 * * * * ?") // every second
    @SchedulerLock(name = "some_task")
    fun runSomeTask() {
        println("some task")
    }
}

fun main(args: Array<String>) {
    runApplication<CqlSessionInjection>(*args)
}

build.gradle

buildscript {
    ext {
        kotlinVersion = '1.3.71'
        springBootVersion = '2.2.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
    }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.foo'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'net.javacrumbs.shedlock', name: 'shedlock-spring', version: '4.9.1'
    implementation group: 'net.javacrumbs.shedlock', name: 'shedlock-provider-cassandra', version: '4.9.1'

    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-cassandra'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    implementation group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8'
    implementation group: 'org.jetbrains.kotlin', name: 'kotlin-reflect'
    implementation group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.3.3'

    compile group: 'com.google.guava', name: 'guava', version: '23.3-jre'

    testCompile group: 'org.cassandraunit', name: 'cassandra-unit-spring', version: '3.11.2.0'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
    annotationProcessor group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
}

task downloadDependencies(type: Exec) {
    configurations.testRuntime.files
    commandLine 'echo', 'Downloaded all dependencies'
}

compileKotlin.dependsOn(processResources)
compileJava.dependsOn(processResources)

settings.gradle

rootProject.name = 'CqlSessionInejction'

src/test/kotlin/com/foo/cqlsessioninjection/integration/ContextTest.kt

package com.foo.cqlsessioninjection.integration

import org.cassandraunit.spring.CassandraDataSet
import org.cassandraunit.spring.CassandraUnitDependencyInjectionTestExecutionListener
import org.cassandraunit.spring.EmbeddedCassandra
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.TestExecutionListeners
import org.springframework.test.context.junit4.SpringRunner

@RunWith(SpringRunner::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestExecutionListeners(
    listeners = [CassandraUnitDependencyInjectionTestExecutionListener::class],
    mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
)
@CassandraDataSet(value = ["cql/cql_session_injection.cql"], keyspace = "shedlock")
@EmbeddedCassandra
class ContextTest {
    @Test
    fun `test task`() {
        println("starting to sleep")
        Thread.sleep(4000)
        println("done sleeping")
    }
}

src/test/resources/application.yml

spring:
  data:
    cassandra:
      contact-points: localhost
      port: 9142
      keyspace_name: shedlock

src/test/resources/cql/cql_session_injection.cql

DROP KEYSPACE IF EXISTS shedlock;

CREATE KEYSPACE shedlock
WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':1};

CREATE TABLE shedlock.lock (
    name text PRIMARY KEY,
    lockUntil timestamp,
    lockedAt timestamp,
    lockedBy text
);

1 Ответ

2 голосов
/ 03 мая 2020

Do c, на который вы ссылались, указывает на то, что бин CqlSession должен быть создан, но он на самом деле не объясняет, как вы туда попали. Я хотел бы сообщить об этом как о проблеме с документацией для этого проекта, чтобы сделать его немного более явным.

Вам необходим Spring Boot 2.3, чтобы получить поддержку Cassandra v4. Используемая версия (2.2x) использует Cassandra v3 и совершенно другой API.

...