RabbitMQ播放模块! 构架

RabbitMQ提供了具有可预测且一致的吞吐量和延迟的高可用性,可伸缩和便携式消息系统。 RabbitMQ是AMQP (业务消息传递的开放标准)的领先实现 ,并且通过适配器支持XMPP,SMTP,STOMP和HTTP来进行轻量级Web消息传递。

这个新模块允许您在Play的RabbitMQ实例上使用和产生消息! 框架应用程序。

安装

play install rabbitmq

组态

module.rabbitmq=${play.path}/modules/rabbitmq-0.0.1
rabbitmq.host=localhost
rabbitmq.port=5672
rabbitmq.userName=guest
rabbitmq.password=guest
rabbitmq.vhost=/
rabbitmq.exchangeType=direct
rabbitmq.durable=true
rabbitmq.autoAck=false
rabbitmq.basicQos=true

定义将由队列使用的消息(只是一个简单的POJO)

public class SampleMessage implements Serializable {

    /** The field1. */
    private String field1;

    /** The field2. */
    private String field2;

    /**
     * Instantiates a new sample message.
     */
    public SampleMessage() {

    }

    /**
     * Instantiates a new sample message.
     *
     * @param field1 the field1
     * @param field2 the field2
     */
    public SampleMessage(String field1, String field2) {
        super();
        this.field1 = field1;
        this.field2 = field2;
    }

    /**
     * Gets the field1.
     *
     * @return the field1
     */
    public String getField1() {
        return field1;
    }

    /**
     * Sets the field1.
     *
     * @param field1 the new field1
     */
    public void setField1(String field1) {
        this.field1 = field1;
    }

    /**
     * Gets the field2.
     *
     * @return the field2
     */
    public String getField2() {
        return field2;
    }

    /**
     * Sets the field2.
     *
     * @param field2 the new field2
     */
    public void setField2(String field2) {
        this.field2 = field2;
    }

    /**
     * To String
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "SampleMessage [field1=" + field1 + ", field2=" + field2 + "]";
    }
}

发布消息

public static void publish(String q) {
     RabbitMQPublisher.publish("myQueue", new SampleMessage(q, q));
     render(q);
    }

创建消息使用者

@OnApplicationStart(async=true)
public class RabbitMQSampleConsumer extends RabbitMQConsumer {

    /**
     * Consume Message
     *
     * @see play.modules.rabbitmq.consumer.RabbitMQConsumer#consume(T)
     */
    @Override
    protected void consume(SampleMessage message) {
        System.out.println("******************************");
        System.out.println("* Message Consumed: " + message);
        System.out.println("******************************");
    }

    /**
     * Name of the Queue that this consumer will be listening to.
     *
     * @return the string
     * @see play.modules.rabbitmq.consumer.RabbitMQConsumer#queue()
     */
    @Override
    protected String queue() {
        return "myQueue";
    }

    /**
     * Return message type.
     *
     * @return the message type
     * @see play.modules.rabbitmq.consumer.RabbitMQConsumer#getMessageType()
     */
    protected Class getMessageType() {
        return SampleMessage.class;
    }
}

*请注意,这是一场戏! 作业,因此您可以手动启动它,也可以使用Play提供的其他注释! 例如@On或@Every。 有关更多信息,请参见“ 异步作业”文档

Firehose –另一种批量发布消息的方法

@OnApplicationStart(async = true)
public class RabbitMQSampleFirehose extends RabbitMQFirehose {

    /** The count. */
    public int count = 0;

    /**
     * Get data to be loaded.
     *
     * @param n the n
     * @return the data
     * @throws Exception the exception
     * @see play.modules.rabbitmq.producer.RabbitMQFirehose#getData(int)
     */
    @Override
    protected List getData(int n) throws Exception {
        if ( count >= 10 ) {
            return null;
        }
        List results = new ArrayList();
        for (int i = 0; i < n; i++) {
            results.add(new SampleMessage("field1", "field2"));
            count++;
        }
        return results;
    }

    /**
     * Batch Size - How many records we will select at the time?.
     *
     * @return the int
     * @see play.modules.rabbitmq.producer.RabbitMQFirehose#batchSize()
     */
    @Override
    protected int batchSize() {
        return 2;
    }

    /**
     * Queue Name.
     *
     * @return the string
     * @see play.modules.rabbitmq.producer.RabbitMQFirehose#queueName()
     */
    @Override
    protected String queueName() {
        return "myQueue";
    }

}

*请注意,这是一场戏! 作业,因此您可以手动启动它,也可以使用Play提供的其他注释! 例如@On或@Every。 有关更多信息,请参见“ 异步作业”文档 。 当然,该代码可在Github上获得

现在开始游戏!

参考: RabbitMQ Play模块! 来自JCG合作伙伴 Felipe OliveiraGeeks的 框架 完全在

相关文章:


翻译自: https://www.javacodegeeks.com/2011/04/rabbitmq-module-play-framework.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值