利用jms消息选择器过滤消息

1. 消息选择器是根据 header 和 properties 允许客户端选择性的制定需要接收的消息

注意 消息选择器是无法利用 消息主题(Body)进行过滤的. 无论你的消息主题是什么类型. 文本.或者对象,或者键值对

根据消息选择器定义的条件,如果为true ,例如 消息头或者属性值 为某个标识符 ,这个时候表示接收消息.

否则 ,如果选择器为空 则为不过滤消息.默认接收所有该通道的消息.该消息过滤类似SQL语句.不过还是要准从一定的约束

消息过滤才起作用. 例如: 过滤 usercode 不等于 admin的消息. 应该这样定义: usercode<>'admin', 而不能写

usercode!='admin' , 毕竟他没有sql这样强大的容错功能.

2. 消息过滤字串默认是不区分大小写的.并且过滤条件的优先级是按照从左至右的顺序, 不过你可以利用 ()

 

来改变消息的优先级

比如: 我要接收 usercode 不等于 admin 或者 接收 当usercode是admin的时候并且receiver等于you的 消息

我们可以这样定义 selector = usercode<>'admin' or (usercode='admin' and receiver='you')

3.下面我们就消息选择器的规范和可定义范围说明下

  • 可接收类型:
    • 字符串 , 利用单引号包含字符串表示条件值
    • 整形, 是不包含小数点的正整数或者负整数
    • 浮点型, 正浮点型或者负浮点型 比如: -98.0232 或者 4.234 等
    • 布尔常量 TURE 或者 FALSE . 注意 如果用字符串表示 是不区分大小写的. 比如: isDestroy =  true
  • 属性标识符定义:
    • 其实说白了就跟java 定义变量名一样.字母开头. 或者 _ $ 等等
    • 不能用 NULL , TRUE FALSE 等定义. 这些就不多说了
    • 不能用 NOT , AND , OR , BETWEEN , LIKE , IN , IS , 或者 ESCAPE 定义
    • 要么在heads中定义 要么在 property中定义. 如果双方约束是利用properties中定义. 但是接收没有,它不会自定去头引用中查找. 直接会是空
    • 根据属性定义的类型去相应的取值 , 例如这里将 2 当做字符串放入了属性体重.就应该这样取
      myMessage.setStringProperty("NumberOfOrders", "2");
      如果这样取的话.就会报异常. 我就发愁 , 应该不会有人这样搞吧
      "NumberOfOrders > 1"
    • 属性标识符是区分大小写的 .(这个是区分的)
    • 任何以 JMSX 开头的 都是属性定义
    • 任何以 JMS_ 开头的 都是provider-specific 属性名称
    • 任何不以 JMS 开头的 属性定义 都是一个 application-specific 属性名称
  • 空格,换行等. 相应参考java 属性定义
  • 表达式:
    • 可以是条件表达式
    • 可以是算术表达式
    • 可以是比较运算和逻辑运算组成的表达式
  • 支持 () 左右括号
  • 支持逻辑运算的优先顺序表达式 例如: NOT , AND , OR
  • 比较运算符有: = , > , >= , < , <= , <> (not equal)
    • 根据类型相应的利用运算符比较
    • 字符串和布尔比较 只支持 = 或者 <>
  • 算术运算符的优先顺序:
    • + , - (unary)
    • * , / (multiplication and division)
    • + , - (addition and subtraction) 内陆运输
    • 算术运算必须遵循java 语言规范
  • 算术表达式 arithmetic-expr1 (不)介于 arithmetic-expr2 和  arithmetic-expr3 之间 例如: arithmetic-expr1 [NOT] BETWEEN arithmetic-expr2 AND arithmetic-expr3
    • "age BETWEEN 15 AND 19" is equivalent to "age >= 15 AND age <= 19"
    • "age NOT BETWEEN 15 AND 19" is equivalent to "age < 15 OR age > 19"
  • 标识符 [NOT] IN (string-literal1 , string-literal2 ,...)(比较字符串或者 null 是否在其中存在)
    • "Country IN (' UK', 'US', 'France') " is true for 'UK' and false for 'Peru' ;  相当于 "(Country = ' UK') OR (Country = ' US') OR (Country = ' France') "
    • "Country NOT IN (' UK', 'US', 'France') " is false for 'UK' and true for 'Peru' ; 相当于 "NOT ((Country = ' UK') OR (Country = ' US') OR (Country = ' France')) "
    • If identifier of an IN or NOT IN operation is NULL , the value of the operation is unknown.
  • identifier [NOT] LIKE pattern-value [ESCAPE escape-character ] (comparison operator, where identifier has a String value; pattern-value is a string literal where '_' stands for any single character; '%' stands for any sequence of characters, including the empty sequence; and all other characters stand for themselves. The optional escape-character is a single-character string literal whose character is used to escape the special meaning of the '_' and '%' in pattern-value .)
    • "phone LIKE '12%3'" is true for '123' or '12993' and false for '1234'
    • "word LIKE 'l_se'" is true for 'lose' and false for 'loose'
    • "underscored LIKE '\_%' ESCAPE '\'" is true for '_foo' and false for 'bar'
    • "phone NOT LIKE '12%3'" is false for '123' or '12993' and true for '1234'
    • If identifier of a LIKE or NOT LIKE operation is NULL , the value of the operation is unknown.
  • 标识符是null
    • "prop_name IS NULL"
  • 标识符非空 not null
    • "prop_name IS NOT NULL"

  • JMS providers are required to verify the syntactic correctness of a message selector at the time it is presented. A method that provides a syntactically incorrect selector must result in a JMSException . JMS providers may also optionally provide some semantic checking at the time the selector is presented. Not all semantic checking can be performed at the time a message selector is presented, because property types are not known.

    The following message selector selects messages with a message type of car and color of blue and weight greater than 2500 pounds: 各种路由器设置图解

    "JMSType = 'car' AND color = 'blue' AND weight > 2500"
    Null Values

    As noted above, property values may be NULL . The evaluation of selector expressions containing NULL values is defined by SQL92 NULL semantics. A brief description of these semantics is provided here.

    SQL treats a NULL value as unknown. Comparison or arithmetic with an unknown value always yields an unknown value.

    The IS NULL and IS NOT NULL operators convert an unknown value into the respective TRUE and FALSE values.

    布尔操作符使用三值逻辑,由下表定义: T表示TRUE F表示FALSE U表示UNDEFINE

    AND 运算符对应结果图

     | AND  |   T   |   F   |   U
     +------+-------+-------+-------
     |  T   |   T   |   F   |   U
     |  F   |   F   |   F   |   F
     |  U   |   U   |   F   |   U
     +------+-------+-------+-------
     

    OR 运算符对应结果图

     | OR   |   T   |   F   |   U
     +------+-------+-------+--------
     |  T   |   T   |   T   |   T
     |  F   |   T   |   F   |   U
     |  U   |   T   |   U   |   U
     +------+-------+-------+------- 
     

    NOT运算结果:

     | NOT
     +------+------
     |  T   |   F
     |  F   |   T
     |  U   |   U
     +------+-------
     

转载于:https://www.cnblogs.com/ctou45/archive/2011/08/02/2124709.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值