1

我现在使用的是:spring 4.0.0.RELEASE 和 spring-rabbit 1.4.4.RELEASE

我的代码会处理消息。消息队列并没有消失,所以。一直循环处理。

spring-rabbitmq.xml

<bean id="callBackTaskMessageListener" class="com.ghca.easyview.server.api.rabbitmq.worker.CallBackTaskMessageListener">
        <property name="rabbitTemplate" ref="rabbitTemplate"/>
    </bean>
    <rabbit:listener-container connection-factory="connectionFactory" prefetch="1" concurrency="30">
        <rabbit:listener queue-names="${backFromTask}" ref="callBackTaskMessageListener"/>
    </rabbit:listener-container>

CallBackTaskMessageListener.java

public class CallBackTaskMessageListener implements MessageListener {

    private static MessageConverter messageConverter = new SimpleMessageConverter();

    private RabbitTemplate rabbitTemplate;

    public RabbitTemplate getRabbitTemplate() {
        return rabbitTemplate;
    }

    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Override
    public void onMessage(Message requestMessage) {
        String message = null;
        AbstractCallBack callBackObj = null;
        try {
            message = new String(requestMessage.getBody(), "utf-8");
            if (StringUtils.isBlank(message)) {
                throw new CallBackDataException("not null or ''.");
            }
            JSONObject callBackData = new JSONObject();
            JSONObject jsonObject = JSON.parseObject(message);
            LogUtils.logInfo("current callBack Data is : "+ jsonObject.toJSONString());
            String code = jsonObject.getString("code");
            if (StringUtils.isNotBlank(code)) {
                JSONObject data = jsonObject.getJSONObject("data");
                callBackData.put("data", data);
            }
            callBackData.put("code", code);
            callBackData.put("message", jsonObject.get("message"));
            String callBackIdentify = jsonObject.getString("moduleName");
            String[] classIndetifyAndMethodName = callBackIdentify.split("&");
            callBackObj = CallBackFactory.createCallBack(CallBack.class.getSimpleName());
            Method callMethod = callBackObj.getClass().getMethod("call", String.class,String.class,String.class);
            callMethod.invoke(callBackObj, callBackData.toJSONString(),classIndetifyAndMethodName[0],classIndetifyAndMethodName[1]);
        } catch (Exception e) {
            LogUtils.logError(callBackObj.getClass().getName() + "->error.",e);
        } finally {
            //rabbitTemplate.getMessageConverter().
        }
    }
}

如何解决 不要让他一直处理栏目一些消息。如何处理消息正确响应?谢谢。

4

0 回答 0