首页 > 代码库 > metaq spring

metaq spring

spring metaq

spring bean 配置

  <bean id="sessionFactory" class="com.taobao.metamorphosis.client.extension.spring.MetaqMessageSessionFactoryBean">
        <property name="zkConnect" value="192.168.137.2:2181"/>
        <property name="zkSessionTimeoutMs" value="30000"/>
        <property name="zkConnectionTimeoutMs" value="30000"/>
        <property name="zkSyncTimeMs" value="5000"/>
    </bean>

    <bean id="messageBodyConverter"  class="com.taobao.metamorphosis.client.extension.spring.JavaSerializationMessageBodyConverter"/>

     <bean id ="metaqTemplate" class="com.taobao.metamorphosis.client.extension.spring.MetaqTemplate">    
        <property name="messageSessionFactory" ref="sessionFactory"/>
        <property name="messageBodyConverter" ref="messageBodyConverter"/>
    </bean>

使用

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.taobao.metamorphosis.client.extension.spring.MessageBuilder;
import com.taobao.metamorphosis.client.extension.spring.MetaqTemplate;

@Service("topicService")
public class TopicService {

    @Autowired
    @Qualifier("metaqTemplate")
    private MetaqTemplate metaqTemplate;

    public MetaqTemplate getMetaqTemplate() {
        return metaqTemplate;
    }

    public void setMetaqTemplate(MetaqTemplate metaqTemplate) {
        this.metaqTemplate = metaqTemplate;
    }

    public void sendMsg(String topic, String msg) {
        try {

            metaqTemplate.send(MessageBuilder.withTopic(topic).withBody(new Date()));

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

metaq spring