首页 \ 问答 \ 如何在Spring Boot和RabbitMQ中配置和接收和转换jSON负载到域对象中(How to configure and receiveAndConvert jSON payload into domain Object in Spring Boot and RabbitMQ)

如何在Spring Boot和RabbitMQ中配置和接收和转换jSON负载到域对象中(How to configure and receiveAndConvert jSON payload into domain Object in Spring Boot and RabbitMQ)

最近我一直对使用Spring Boot的微服务架构有浓厚的兴趣。 我的实现有两个Spring引导应用程序;

Application One从RESTful API接收请求,将jSON有效负载转换并发送到RabbitMQ队列A.

应用程序二 ,已订阅queueA ,接收jSON负载(域对象用户),并且应该在应用程序二中激活服务,例如。 发送电子邮件给用户。

在我的应用程序两个配置中不使用XML,我如何配置转换器将转换从RabbitMQ接收到的jSON有效载荷转换为域对象用户。

以下是Application Two上Spring Boot配置的片段

Application.class

@SpringBootApplication
@EnableRabbit
public class ApplicationInitializer implements CommandLineRunner {

    final static String queueName = "user-registration";

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    AnnotationConfigApplicationContext context;

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange("user-registrations");
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueName);
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    public static void main(String[] args) {
        SpringApplication.run(ApplicationInitializer.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Waiting for messages...");
    }

}

TestService.java

@Component
public class TestService {

    /**
     * This test verifies whether this consumer receives message off the user-registration queue
     */
    @RabbitListener(queues = "user-registration")
    public void testReceiveNewUserNotificationMessage(User user) {
        // do something like, convert payload to domain object user and send email to this user
    }

}

Recently I have been having a keen interest on Microservice Architecture using Spring Boot. My implementation has two Spring boot applications;

Application One receives requests from a RESTful API, converts and sends jSON payload to a RabbitMQ queueA.

Application Two, has subscribed to queueA, receives the jSON payload(Domain Object User) and is supposed to activate a service within Application Two eg. send email to a user.

Using no XML in my Application Two configuration, how do I configure a converter that will convert the jSON payload received from RabbitMQ into a Domain Object User.

Below are snippets from Spring Boot configurations on Application Two

Application.class

@SpringBootApplication
@EnableRabbit
public class ApplicationInitializer implements CommandLineRunner {

    final static String queueName = "user-registration";

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    AnnotationConfigApplicationContext context;

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange("user-registrations");
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueName);
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    public static void main(String[] args) {
        SpringApplication.run(ApplicationInitializer.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Waiting for messages...");
    }

}

TestService.java

@Component
public class TestService {

    /**
     * This test verifies whether this consumer receives message off the user-registration queue
     */
    @RabbitListener(queues = "user-registration")
    public void testReceiveNewUserNotificationMessage(User user) {
        // do something like, convert payload to domain object user and send email to this user
    }

}

原文:https://stackoverflow.com/questions/29592543
更新时间:2022-09-06 08:09

最满意答案

如果您不关心书架上书的顺序,您的答案是正确的。 如果您将两组中每本书的不同排序计算为填充书架的不同方式,那么您的教师答案是正确的。


Your answer is correct if you don't care what order the books go on the shelf. Your instructor's answer is correct if you count different orderings of the books within each of the two groups as different ways of filling the bookshelf.

相关问答

更多
  • 学技术可以考虑学厨师,现在厨师比较好找工作,厨师未来几年需求量都是很大的,而且厨师工资高,工作稳定,还能创业。特别是现在的川菜,是非常热门的,中国第一大菜系,学个一年两年出来上班,只要技术好,川菜厨师现在都是抢着要。
  • 我将用W,W,W,B,B和G表示每种颜色的兔子。 首先请注意,您最多将使用二项式(3,3)×二项式(3,2)×二项式(1,1)= 60这样的对齐。 因为这不是一个如此之大的数字,并且因为“不是两只相同颜色的兔子并排”的约束非常强烈(因为你的一半兔子已经拥有完全相同的颜色!),让我们试着一下所有这些正确的线条。 首先,你有一条线,你只有一条白色的兔子在一条线的边缘。 这意味着线必须看起来像(假设白兔开始行)W _ W _ W _,就是这样。 现在你可以用剩余的兔子填充你想要的空点_,并且所有颜色的约束都会得到 ...
  • 那是你要的吗? In [38]: from itertools import combinations In [40]: df Out[40]: Sn.No Series_A Series_B Series_C 0 1 10 17 12 1 2 11 13 15 2 3 13 15 13 In [41]: cols = df.filter ...
  • 我得到了我想要的东西 它来自@mudasobwa链接。 我编辑我想要的东西。
  • 只有很多方法可以将FSS包含在七个字符的字符串中。 FSS#### #FSS### ##FSS## ###FSS# ####FSS 因此,有五种不同的牌照,其中包含FSS字符串。 如果四个数字没有约束,则表示每个“FSS”位置有9,999个不同的牌照。 您可能希望从总答案中减去9,999 * 5以获得允许的牌照。 编辑:所以你想要在第一,第五,第六和第七位置的所有排列0-9。 第二,第三和第四个位置的AZ的所有排列,除了第二个中的F,第三个中的S和第四个中的S,右边? 如果是,则为10 * 25 * 25 ...
  • 如果您不关心书架上书的顺序,您的答案是正确的。 如果您将两组中每本书的不同排序计算为填充书架的不同方式,那么您的教师答案是正确的。 Your answer is correct if you don't care what order the books go on the shelf. Your instructor's answer is correct if you count different orderings of the books within each of the two groups ...
  • 为了更简短的解释和更普遍(正如你提到的行数可以更大10)我建议你可以自己实现以下逻辑:开始时,初始设置。 n <- 10 k <- 3 df <- as.data.frame( lapply(1:n, function(x) x <- k:1 ) ) 下面的代码创建了10个元素集的所有唯一组合,其中顺序无关紧要。 all <- as.matrix( expand.grid(df) ) all_sorted <- t( apply(all, 1, sort) ) all_sorted_unique <- ...
  • 我只是为了好玩而去做这件事,实际上这是一个小挑战,因为一个幼稚的实现很快就会溢出。 我在评论中包含了这些内容。 方程 nPr = n! / (n - r)! nCr = n! / r! (n - r)! Implementaion public static class PermutationsAndCombinations { public static long nCr(int n, int r) { // naive: return Factorial(n) / (F ...
  • 您可以使用itertools.product >>>from itertools import product >>>list(product([0,1],repeat=5)) You can use itertools.product >>>from itertools import product >>>list(product([0,1],repeat=5))
  • 如果团队中的玩家数量是主要权力(2,3,4,5,7,8,9,11,13,16,17,19等),那么这里有一个算法可以创建一个时间表最大轮数,基于有限仿射平面。 我们在有限域GF (n)中工作,其中n是团队中的球员数量。 GF(n)有自己的乘法概念; 当n是素数时,它是乘法mod n,当n是某个素数的较高幂时,它是单变量多项式的乘法,是一个适当程度的不可约多项式。 每个团队由GF(n)的非零元素标识; 让团队标识符集为T.每个团队成员由T×GF(n)中的一对标识。 对于GF(n)的每个非零元素r,圆r的组是 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)