栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过Java与Django / Celery互操作

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过Java与Django / Celery互操作

我找到了解决方案。RabbitMQ的Java库引用交换/队列/路由键。在Celery中,队列名称实际上是映射到Java库中引用的交换。默认情况下,芹菜的队列只是“芹菜”。如果您的Django设置使用以下语法定义了一个名为“
myqueue”的队列:

CELERY_ROUTES = {    'mypackage.myclass.runworker'      : {'queue':'myqueue'},}

然后,基于Java的代码需要执行以下操作:

        ConnectionFactory factory = new ConnectionFactory();        Connection connection = null ;        try { connection = factory.newConnection(mqHost, mqPort);        } catch (IOException ioe) { log.error("Unable to create new MQ connection from factory.", ioe) ;        }        Channel channel = null ;        try { channel = connection.createChannel();        } catch (IOException ioe) { log.error("Unable to create new channel for MQ connection.", ioe) ;        }        try { channel.queueDeclare("celery", false, false, false, true, null);        } catch (IOException ioe) { log.error("Unable to declare queue for MQ channel.", ioe) ;        }        try { channel.exchangeDeclare("myqueue", "direct") ;        } catch (IOException ioe) { log.error("Unable to declare exchange for MQ channel.", ioe) ;        }        try { channel.queueBind("celery", "myqueue", "myqueue") ;        } catch (IOException ioe) { log.error("Unable to bind queue for channel.", ioe) ;        } // Generate the message body as a string here.        try { channel.basicPublish(mqExchange, mqRouteKey,      new AMQP.BasicProperties("application/json", "ASCII", null, null, null, null, null, null, null, null, null, "guest", null, null),     messageBody.getBytes("ASCII"));        } catch (IOException ioe) { log.error("IOException encountered while trying to publish task via MQ.", ioe) ;        }

事实证明,这只是术语上的差异。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/497644.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号