ASP.NET ActiveMQ 消息队列
1.引入
2.发送消息
3.接收消息
概述:MQ消息存放在内存,重启后,消息丢失。接收后,消息丢失(只取一次),不取,一直在且速度快。
使用前:下载apache-activemq-5.15.2-bin ,下载完运行activemq.bat(必须提前安装Java sdk)
注册成功后:http://localhost:8161/admin/ 用户名:admin 密码:admin 登录



一 引入DLL

二 发送消息、接收消息
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WinformQuartz
{
public partial class FrmMQ : Form
{
public FrmMQ()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
var input = this.textBox1.Text.Trim(); if(string.IsNullOrEmpty(input))
{
this.lblMsg.ForeColor = Color.Red;
this.lblMsg.Text = "请输入需要发送的消息";
return;
} Send();
} public void Send()
{
ConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");
//通过工厂建立连接
using (IConnection connection = factory.CreateConnection())
{
//通过连接创建Session会话
using (ISession session = connection.CreateSession())
{
//通过会话创建生产者,方法里面new出来的是MQ中的Queue
IMessageProducer prod = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue"));
//创建一个发送的消息对象
ITextMessage message = prod.CreateTextMessage();
//给这个对象赋实际的消息
message.Text = this.textBox1.Text.Trim();
//设置消息对象的属性,这个很重要哦,是Queue的过滤条件,也是P2P消息的唯一指定属性
message.Properties.SetString("filter", "demo");
//生产者把消息发送出去,几个枚举参数MsgDeliveryMode是否长链,MsgPriority消息优先级别,发送最小单位,当然还有其他重载
prod.Send(message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);
this.lblMsg.Text = "发送成功!!";
}
}
} public void Recieve()
{
//创建连接工厂
IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");
//通过工厂构建连接
IConnection connection = factory.CreateConnection();
//这个是连接的客户端名称标识
connection.ClientId = "firstQueueListener";
//启动连接,监听的话要主动启动连接
connection.Start();
//通过连接创建一个会话
ISession session = connection.CreateSession();
//通过会话创建一个消费者,这里就是Queue这种会话类型的监听参数设置
IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue"), "filter='demo'");
//注册监听事件
consumer.Listener += new MessageListener(consumer_Listener);
//connection.Stop();
//connection.Close();
} void consumer_Listener(IMessage message)
{
ITextMessage msg = (ITextMessage)message;
//异步调用下,否则无法回归主线程
this.textBox2.Invoke(new DelegateRevMessage(RevMessage), msg); } public delegate void DelegateRevMessage(ITextMessage message); public void RevMessage(ITextMessage message)
{
this.textBox2.Text += string.Format(@"接收到:{0}{1}", message.Text, Environment.NewLine);
} private void button2_Click(object sender, EventArgs e)
{
Recieve();
}
}
}


ASP.NET ActiveMQ 消息队列的更多相关文章
- activemq消息队列的使用及应用docker部署常见问题及注意事项
activemq消息队列的使用及应用docker部署常见问题及注意事项 docker用https://hub.docker.com/r/rmohr/activemq/配置在/data/docker/a ...
- JAVA的设计模式之观察者模式----结合ActiveMQ消息队列说明
1----------------------观察者模式------------------------------ 观察者模式:定义对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的 ...
- ActiveMQ消息队列从入门到实践(4)—使用Spring JMS收发消息
Java消息服务(Java Message Service ,JMS)是一个Java标准,定义了使用消息代理的通用API .在JMS出现之前,每个消息代理都有私有的API,这就使得不同代理之间的消息代 ...
- ASP.NET Core消息队列RabbitMQ基础入门实战演练
一.课程介绍 人生苦短,我用.NET Core!消息队列RabbitMQ大家相比都不陌生,本次分享课程阿笨将给大家分享一下在一般项目中99%都会用到的消息队列MQ的一个实战业务运用场景.本次分享课程不 ...
- C#实现ActiveMQ消息队列
本文使用C#实现ActiveMQ消息队列功能. 一.首先需要导入两个包,分别是:Apache.NMS 和 Apache.NMS.ActiveMQ 二.创建Winform程序实现生产者功能. 三.Pro ...
- SpringBoot集成ActiveMq消息队列实现即时和延迟处理
原文链接:https://blog.csdn.net/My_harbor/article/details/81328727 一.安装ActiveMq 具体安装步骤:自己谷歌去 二.新建springbo ...
- ActiveMQ 消息队列服务
1 ActiveMQ简介 1.1 ActiveMQ是什么 ActiveMQ是一个消息队列应用服务器(推送服务器).支持JMS规范. 1.1.1 JMS概述 全称:Java Message Serv ...
- ActiveMQ基础教程(四):.net core集成使用ActiveMQ消息队列
接上一篇:ActiveMQ基础教程(三):C#连接使用ActiveMQ消息队列 这里继续说下.net core集成使用ActiveMQ.因为代码比较多,所以放到gitee上:https://gitee ...
- ActiveMQ消息队列的使用及应用
这里就不说怎么安装了,直接解压出来就行了. 谢绝转载,作者保留所有权力 目录: 一:JMQ的两种消息模式 1.1:点对点的消息模式 1.2:订阅模式 二:点对点的实现代码 2.1:点对点的发送端 2 ...
随机推荐
- html页面边框的另一种写法
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Python 简单分页思路
一: li = [] for i in range(1000): li.append(i) while True: p = input('input page: ') p = int(p) start ...
- 【读书笔记】iOS-开发者证书
虽然使用通配符听起来很赞,但问题是使用这种App ID的应用无法使用苹果的Push Notification服务以及应用内支付服务. 如果你有钱的话,建议单独买一个设备用于开发,将来你可能会安装iOS ...
- python之继承
1.经典MRO : 树形结构的深度遍历优先 - > 树形结构遍历 class A: pass class B(A): pass class C(A): pass class D(B, C): p ...
- 1145.cn 百度MIP适配实例
MIP,全称Mobile Instant Pages(移动端即时页面),是百度推出的一套移动端网页开放技术标准.网站移动端页面统计MIP改造,能实现页面缓存,从而达到移动网页加速效果. 百度官方已经明 ...
- css中那些属性可以被继承
主要的有: 字体相关:line-height, font-family, font-size, font-style, font-variant, font-weight, font 文本相关: le ...
- 最新安全狗 apache v4.0 sql注入 bypass
前言 最近没事学习一下 waf 的 bypass , 本文介绍下 bypass 安全狗的笔记.个人感觉 bypass 的总思路(正则匹配型 waf)就是利用各种语法特性来逃避正则(当然要保证语法正确性 ...
- Python tab键命令补全
pip install pyreadline import rlcompleter, readline readline.parse_and_bind('tab: complete') root@pe ...
- system.transfer.list版本进化
从android5.0开始之后,recovery升级包中不再升级system.img,而是升级system.new.dat+system.transfer.list的这种文件组合,经过android版 ...
- [20171206]rman与truncate.txt
[20171206]rman与truncate.txt --//昨天下班在回家的路上,突然想起以前遇到的问题,就是truncate表后,rman做备份时会备份多少truncate表的信息,--//当时 ...