Git Product home page Git Product logo

Comments (14)

asm0dey avatar asm0dey commented on April 26, 2024 1

@garyrussell but Spring's RestTemplate knows how to map generics. It can map anything with help of TypeReference

from spring-amqp.

artembilan avatar artembilan commented on April 26, 2024

Would you mind to share all the configuration to play from our side?
Thanks

from spring-amqp.

asm0dey avatar asm0dey commented on April 26, 2024

Let's try to figure out what I have. Sorry, but code is in Kotlin.

Working test:

    @Test
    fun shouldExceptOnWrongData() {
        val deal = secondaryDeal().copy(dealNo = "")
        val requestListener = harness.getSpy<DealListener>("deals")
        requestListener.validator = validator
        val respObject = rabbitTemplate.convertSendAndReceive(props.queueName, deal)
        val respString = mapper.writeValueAsString(respObject)
        val resp = mapper.readValue<ResponseWrapper<Either<String, ErrorInfo>>>(respString, object : TypeReference<ResponseWrapper<Either<String, ErrorInfo>>>() {})
        assertThat(resp.success, `is`(false))
        assertThat(resp.payload.isRight(), `is`(true))
        assertThat(resp.payload.right().code, `is`(400))
        assertThat(resp.payload.right().message, not(isEmptyOrNullString()))
    }

Test class:

@SpringBootTest(classes = arrayOf(ITConfig::class))
open class RequestListenerRabbitIT : BaseRabbitIT() {
    @Autowired
    lateinit var rabbitTemplate: RabbitTemplate
    @Autowired
    lateinit var harness: RabbitListenerTestHarness
    @MockBean
    lateinit var dealService: DealService
    @Autowired
    lateinit var props: DealConfigurationProperties
    @Autowired
    @Qualifier("getValidator")
    lateinit var validator: LocalValidatorFactoryBean
    @Autowired
    lateinit var mapper: ObjectMapper

BaseRabbitIT:

@ActiveProfiles("rabbitIT")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@RunWith(SpringRunner::class)
abstract class BaseRabbitIT

ITConfig:

@Profile("!test")
@Configuration
@EnableAutoConfiguration(
    exclude = arrayOf(
        RabbitAutoConfiguration::class
    )
)
@ComponentScan(basePackages = arrayOf("ru.sberned.sgr.core"))
open class ITConfig

from spring-amqp.

garyrussell avatar garyrussell commented on April 26, 2024

The Jackson2JsonMessageConverter can handle "inner" types in standard "containers" (lists, maps etc); it doesn't understand the generic types contained within some kind of user container such as your ResponseContainer - I believe you will need a custom Jackson2JavaTypeMapper to help it with typing; you are getting a hash map because Jackson doesn't know the type to convert to .

from spring-amqp.

garyrussell avatar garyrussell commented on April 26, 2024

Perhaps, but the AMQP converter conveys type information in headers and it (currently) only handles containers (collections, arrays, maps, etc).

from spring-amqp.

asm0dey avatar asm0dey commented on April 26, 2024

@garyrussell does it work without __TypeId__ header? It should according to documentation, but your thesis makes me think that I don't understand docs correctly.

Also — is it be possible to add support of TypeReference to converSendAndRecieve and recieveAndConvert methods?

from spring-amqp.

artembilan avatar artembilan commented on April 26, 2024

Your code is useless.
I need to see RabbitTemplate and RabbitListenerContainerFactory configuration.
It isn't clear by that code how you deal with JSON...

from spring-amqp.

garyrussell avatar garyrussell commented on April 26, 2024

It requires either headers or a custom class mapper/type mapper.

is it be possible to add...

Anything's possible - it's software after all; feel free to open a new feature/improvement JIRA Issue and, of course, contributions are always welcome.

from spring-amqp.

asm0dey avatar asm0dey commented on April 26, 2024

@artembilan
ok, sorry. Here's Rabbit config

@Configuration
@Conditional(TestProfileNotActiveCondition::class)
open class RabbitMQConfig {

    @Autowired
    lateinit var connectionFactory: ConnectionFactory

    @Autowired
    lateinit var props: MessagingConfigurationProperties

    @Bean
    open fun admin() = RabbitAdmin(connectionFactory)

    @Bean open fun queue() = Queue(props.deal.queueName, true)
    @Bean open fun queueReply() = Queue("${props.deal.queueName}.reply", true)

    @Bean
    open fun messageConverter(mapper: ObjectMapper) = run {
        val result = Jackson2JsonMessageConverter()
        result.setJsonObjectMapper(mapper)
        result
    }

    @Bean
    open fun contractRequestStateDurableQueue() = Queue(props.event.queueName, true)

    @Bean
    open fun smsQueue() = Queue(props.sms.queueName, true)

    @Bean
    @Qualifier("smsChannel")
    open fun internalChannel(connectionFactory: ConnectionFactory): Channel {
        val channel = connectionFactory.createConnection().createChannel(true)

        channel.queueDeclare(props.sms.queueName, true, false, false, null)

        val args = HashMap<String, Any>()
        args.put("x-delayed-type", "direct")
        channel.exchangeDeclare(props.sms.exchangeName, "x-delayed-message", true, false, args)
        channel.queueBind(props.sms.queueName, props.sms.exchangeName, "")
        return channel
    }
}

There is no anything special as you can see.
So everything else is contained in
compile 'org.springframework.boot:spring-boot-starter-amqp', which is included with spring-boot 1.4.2

from spring-amqp.

garyrussell avatar garyrussell commented on April 26, 2024

@asm0dey Proof of concept here.

We are about to release our first milestone for 2.0 and this needs a lot more work, so it won't make it into that; we should be able to get something in the next milestone at the end of the year, though.

However, we could make the small change now to the RabbitTemplate that supports subclassing so you could write your own template subclass.

Would that be useful?

from spring-amqp.

asm0dey avatar asm0dey commented on April 26, 2024

@garyrussell
1st: thank you very mech for PoC. It looks very promising and I'm very glad that you are worknig on it!
2nd: What do you mean when you're speaking about template subclass? That it'll be possible to write RabbitTemplate, which supports generics? With ParametryzedTypeReference I think?

from spring-amqp.

garyrussell avatar garyrussell commented on April 26, 2024

I am just saying we don't have time to get the full solution in the upcoming milestone.

As a work-around, we could go ahead and expose the

protected Message convertSendAndReceiveRaw(...)

method now.

This would allow users to subclass the template like I do in the PoC with just a small amount of code. We could even do that in the next 1.6.x release.

Without that change to the template, you would have to write much more code.

from spring-amqp.

asm0dey avatar asm0dey commented on April 26, 2024

@garyrussell very cool! Thank you, I think it's very good start!

from spring-amqp.

artembilan avatar artembilan commented on April 26, 2024

This has been fixed via https://jira.spring.io/browse/AMQP-675

from spring-amqp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.