rabbitMq
创建连接
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.autoconfigure.amqp.RabbitProperties; import org.springframework.stereotype.Component; @Component public class MqConnectionUtils implements CommandLineRunner{ public static Channel channel; @Value("${component.segment.id}") private String segmentId; @Autowired private RabbitProperties rabbitProperties; public void getConnection() throws Exception { //定义一个链接工厂 ConnectionFactory connectionFactory = new ConnectionFactory(); //设置服务地址 connectionFactory.setHost(rabbitProperties.getHost()); //设定端口,注意,这里RabbitMQ的端口,不是管理页面的端口 connectionFactory.setPort(rabbitProperties.getPort()); //设定用户名 connectionFactory.setUsername(rabbitProperties.getUsername()); //设定密码 connectionFactory.setPassword(rabbitProperties.getPassword()); //设定虚拟访问节点 connectionFactory.setVirtualHost("/"); Connection connection = connectionFactory.newConnection(); //创建一个通道 channel = connection.createChannel(); } @Override public void run(String... args) throws Exception { getConnection(); } }
发送消息
public BaseResponse pushEvent(String data){ BaseResponse response = new BaseResponse(); try { if (MqConnectionUtils.channel == null || !MqConnectionUtils.channel.isOpen()){ mqConnectionUtils.getConnection(); } MqConnectionUtils.channel.basicPublish(tlncExchange,tlncRoutingKey,null,data.getBytes()); log.info("send message to tlnc:{}",data); } catch (Exception e) { log.error("pushEvent error: ",e); response.setCode("-1"); response.setData(e.getMessage()); } return response; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2019-01-05 js 遍历EL表达式 list对象