NutzCN Logo
问答 nutz-integration-rabbitmq
发布于 2766天前 作者 menglinxi 1912 次浏览 复制 上一个帖子 下一个帖子
标签:

消费者怎么创建与使用?
确认和保证投递,有没有封装?
文档有点太简单。。。。

14 回复

当前只提供了channel(),其他木有

欢迎提交你的实现哦

我能直接提交么?要不要开个号给我?

@IocBy(type=ComboIocProvider.class, args={"*js", "ioc/",
        "*anno", "com.tianshi",
        "*tx", // 事务拦截 aop
        "*jedis",
        "*rabbitmq",
        "*async"}) 
ip=61.147.xxx.xxx
port=5672
user_name=testmq
password=testmq
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.IocBean;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
import java.io.IOException;

import static org.nutz.integration.rabbitmq.aop.RabbitmqMethodInterceptor.channel;


import com.rabbitmq.client.AMQP.BasicProperties;
import org.nutz.lang.ExitLoop;
import org.nutz.log.Log;
import org.nutz.log.Logs;

/**
 * Created by Administrator on 2017-05-27.
 */
@IocBean
public class RabbitmqUtil {
    final Log log = Logs.get();
    @Aop("rabbitmq")
    public void publish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException {
        channel().basicPublish(exchange, routingKey, props, body);
    }

    @Aop("rabbitmq")
    public void publish(String routingKey, byte[] body) throws IOException {
        channel().basicPublish("", routingKey, null, body);
    }
    @Aop("rabbitmq")
    public Object[] get(String routingKey) throws IOException {
        final Object[] obj = {new Object()};
        channel().basicConsume(routingKey, new DefaultConsumer(channel()) {
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       BasicProperties properties,
                                       byte[] body)
                      {
                try {
                    obj[0] = QueueUtils.bytesToObject(body);
                }catch (IOException ie)
                {
                    log.error("io异常"+ie.getStackTrace());
                }catch (Exception e)
                {
                    log.error("异常"+e.getStackTrace());
                }
            }
        });
        return obj;
    }
}
<%
    RabbitmqUtil rabbitmqUtil = new RabbitmqUtil();
    rabbitmqUtil.publish("click","hello".getBytes());
    Object[] objects= rabbitmqUtil.get("click");

    out.print(String.valueOf(objects[0]));
%>
Stacktrace:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:580)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.nutz.mvc.NutFilter.nextChain(NutFilter.java:226)
	org.nutz.mvc.NutFilter.doFilter(NutFilter.java:201)
root cause

java.lang.NullPointerException
	com.test.util.RabbitmqUtil.publish(RabbitmqUtil.java:32)
	org.apache.jsp.test_jsp._jspService(test_jsp.java:92)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.nutz.mvc.NutFilter.nextChain(NutFilter.java:226)
	org.nutz.mvc.NutFilter.doFilter(NutFilter.java:201)

测试报空指针异常。。。我配置错误?没能正常注入?

自行new出来的对象,不死才怪

2017-5-31 10:26:8.411 DEBUG [http-bio-8086-exec-8] Get 'rabbitmqUtil'<class com.tianshi.biz.util.RabbitmqUtil>
2017-5-31 10:26:8.411 DEBUG [http-bio-8086-exec-8] 	 >> Load definition name=rabbitmqUtil
2017-5-31 10:26:8.411 DEBUG [http-bio-8086-exec-8] Found IocObject(rabbitmqUtil) in AnnotationIocLoader(packages=[com.tianshi])
2017-5-31 10:26:8.411 DEBUG [http-bio-8086-exec-8] 	 >> Make...'rabbitmqUtil'<class com.tianshi.biz.util.RabbitmqUtil>
2017-5-31 10:26:8.412 DEBUG [http-bio-8086-exec-8] Get 'rabbitmq'<interface org.nutz.aop.MethodInterceptor>
2017-5-31 10:26:8.412 DEBUG [http-bio-8086-exec-8] 	 >> Load definition name=rabbitmq
2017-5-31 10:26:8.412 DEBUG [http-bio-8086-exec-8] Loading define for name=rabbitmq
2017-5-31 10:26:8.413 DEBUG [http-bio-8086-exec-8] Found IocObject(rabbitmq) in RabbitmqIocLoader@1562420428
2017-5-31 10:26:8.413 DEBUG [http-bio-8086-exec-8] 	 >> Make...'rabbitmq'<interface org.nutz.aop.MethodInterceptor>
2017-5-31 10:26:8.413 DEBUG [http-bio-8086-exec-8] Save object 'rabbitmq' to [app] 
2017-5-31 10:26:8.426 DEBUG [http-bio-8086-exec-8] Get 'rabbitmq_cf'<>
2017-5-31 10:26:8.426 DEBUG [http-bio-8086-exec-8] 	 >> Load definition name=rabbitmq_cf
2017-5-31 10:26:8.427 DEBUG [http-bio-8086-exec-8] Loading define for name=rabbitmq_cf
2017-5-31 10:26:8.427 DEBUG [http-bio-8086-exec-8] Found IocObject(rabbitmq_cf) in RabbitmqIocLoader@1562420428
2017-5-31 10:26:8.427 DEBUG [http-bio-8086-exec-8] 	 >> Make...'rabbitmq_cf'<>
2017-5-31 10:26:8.430 DEBUG [http-bio-8086-exec-8] Load class com.rabbitmq.client.ConnectionFactory without AOP
2017-5-31 10:26:8.430 DEBUG [http-bio-8086-exec-8] Save object 'rabbitmq_cf' to [app] 
2017-5-31 10:26:8.431 DEBUG [http-bio-8086-exec-8] Get 'conf'<>
2017-5-31 10:26:8.438 DEBUG [http-bio-8086-exec-8] Remove object 'rabbitmq_cf' from [app] 
2017-5-31 10:26:8.439 DEBUG [http-bio-8086-exec-8] Remove object 'rabbitmq' from [app] 
org.nutz.ioc.IocException: [rabbitmqUtil, rabbitmq, rabbitmq_cf] # FAIL to create Ioc Bean name=[rabbitmq_cf]
	org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
	org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
	org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
	org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
	org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
	org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	org.nutz.ioc.aop.config.impl.AnnotationAopConfigration.makeIt(AnnotationAopConfigration.java:23)
	org.nutz.ioc.aop.config.impl.AnnotationAopConfigration.makeIt(AnnotationAopConfigration.java:18)
	org.nutz.ioc.aop.SimpleAopMaker.getInterceptorPairList(SimpleAopMaker.java:117)
	org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:64)
	org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:36)
	org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)

报错信息没贴全

14: <body>
15: <%
16: 
17:     RabbitmqUtil rabbitmqUtil = Mvcs.getIoc().get(RabbitmqUtil.class);
18:     rabbitmqUtil.publish("click","hello".getBytes());
19:     Object[] objects= rabbitmqUtil.get("click");
20: 


Stacktrace:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:580)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.nutz.mvc.NutFilter.nextChain(NutFilter.java:226)
	org.nutz.mvc.NutFilter.doFilter(NutFilter.java:201)
root cause

org.nutz.ioc.IocException: [rabbitmqUtil, rabbitmq, rabbitmq_cf] # FAIL to create Ioc Bean name=[rabbitmq_cf]
	org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
	org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
	org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
	org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
	org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
	org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	org.nutz.ioc.aop.config.impl.AnnotationAopConfigration.makeIt(AnnotationAopConfigration.java:23)
	org.nutz.ioc.aop.config.impl.AnnotationAopConfigration.makeIt(AnnotationAopConfigration.java:18)
	org.nutz.ioc.aop.SimpleAopMaker.getInterceptorPairList(SimpleAopMaker.java:117)
	org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:64)
	org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:36)
	org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)
	org.apache.jsp.test_jsp._jspService(test_jsp.java:95)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.nutz.mvc.NutFilter.nextChain(NutFilter.java:226)
	org.nutz.mvc.NutFilter.doFilter(NutFilter.java:201)
root cause

依然不全, 放在入口方法里面测, 别放在jsp里面

找到问题了,是缺了 slf4jd的jar包

这里面还需要增加 rabbitmq.js 么? 配置文件没生效,一直链接 localhost

你应该定义conf这个bean了吧?? 在它的扫描路径内添加rabbitmq.properties

rabbitmq.host=你需要的ip/域名
添加回复
请先登陆
回到顶部