
使用RabbitMQ。Java可以使用RabbitMQ的Java客户端库来接收base64编码的消息。
1、引入RabbitMQ的Java客户端库,可以使用Maven或Gradle等构建工具来管理依赖。
2、创建一个连接工厂对象,并设置连接参数。
3、创建一个连接对象,并从连接对象中获取一个通道对象。
4、声明一个队列,并设置队列参数。
5、创建一个消费者对象,并实现消费者的回调方法。
6、启动消费者,并将消费者绑定到队列上。
7、关闭通道和连接对象。
下面是RabbitMQ的消息确认机制:“为了确保消息不会丢失,RabbitMQ支持消息确认机制。客户端在接受到消息并处理完后,可以发送一个ack消息给RabbitMQ,告诉它该消息可以安全的删除了。假如客户端在发送ack之前意外死掉了,那么RabbitMQ会将消息投递到下一个consumer客户端。如果有多个consumer客户端,RabbitMQ在投递消息时是轮询的。RabbitMQ如何判断客户端死掉了?唯一根据是客户端连接是否断开。这里没有超时机制,也就是说客户端可以处理一个消息很长时间,只要没断开连接,RabbitMQ就一直等待ack消息。”我现在遇到的问题是这样的:我这边有几条线程去消息队列里取数据,但是会有异常数据导致线程挂掉,就是上边的“客户端在发送ack之前意外死掉了”,RabbitMQ会将消息投递到下一个consumer客户端,这样一条异常数据会把我的所有线程挂掉,我现在想实现这样的功能:如果有异常数据导致进程挂掉,那么我不让RabbitMQ将这条消息投递到下一个consumer客户端,而是放到另一个地方或者另外处理,请问该如何实现呢?
本文主要谈一下密码学中的加密和数字签名 以及其在java中如何进行使用 对密码学有兴趣的伙伴 推荐看Bruce Schneier的著作 Applied Crypotography 在jdk 的发行版本中安全性方面有了很大的改进 也提供了对RSA算法的直接支持 现在我们从实例入手解决问题(本文仅是作为简单介绍)
一 密码学上常用的概念
)消息摘要
这是一种与消息认证码结合使用以确保消息完整性的技术 主要使用单向散列函数算法 可用于检验消息的完整性 和通过散列密码直接以文本形式保存等 目前广泛使用的算法有MD MD SHA jdk 对上面都提供了支持 在java中进行消息摘要很简单 java security MessageDigest提供了一个简易的 *** 作方法
Java代码
/MessageDigestExample javaCopyright /import java security MessageDigest;/单一的消息摘要算法 不使用密码 可以用来对明文消息(如 密码)隐藏保存/public class MessageDigestExample{ public static void main(String[] args) throws Exception{if(args length!= ){ System err println( Usage:java MessageDigestExample text ); System exit( );}byte[] plainText=args[ ] getBytes( UTF );//使用getInstance( 算法 )来获得消息摘要 这里使用SHA 的 位算法MessageDigest messageDigest=MessageDigest getInstance( SHA );System out println( +messageDigest getProvider() getInfo());//开始使用算法messageDigest update(plainText);System out println( Digest: );//输出算法运算结果System out println(new String(messageDigest digest() UTF )); }}
lishixinzhi/Article/program/Java/gj/201311/27287
java中的消息队列
消息队列是线程间通讯的手段:
import javautil
public class MsgQueue{
private Vector queue = null;
public MsgQueue(){
queue = new Vector();
}
public synchronized void send(Object o)
{
queueaddElement(o);
}
public synchronized Object recv()
{
if(queuesize()==0)
return null;
Object o = queuefirstElement();
queueremoveElementAt(0);//or queue[0] = null can also work
return o;
}
}
因为java中是locked by object的所以添加synchronized 就可以用于线程同步锁定对象
可以作为多线程处理多任务的存放task的队列。他的client包括封装好的task类以及thread类
Java的多线程-线程间的通信2009-08-25 21:58
1 线程的几种状态
线程有四种状态,任何一个线程肯定处于这四种状态中的一种:
1) 产生(New):线程对象已经产生,但尚未被启动,所以无法执行。如通过new产生了一个线程对象后没对它调用start()函数之前。
2) 可执行(Runnable):每个支持多线程的系统都有一个排程器,排程器会从线程池中选择一个线程并启动它。当一个线程处于可执行状态时,表示它可能正处于线程池中等待排排程器启动它;也可能它已正在执行。如执行了一个线程对象的start()方法后,线程就处于可执行状态,但显而易见的是此时线程不一定正在执行中。
3) 死亡(Dead):当一个线程正常结束,它便处于死亡状态。如一个线程的run()函数执行完毕后线程就进入死亡状态。
4) 停滞(Blocked):当一个线程处于停滞状态时,系统排程器就会忽略它,不对它进行排程。当处于停滞状态的线程重新回到可执行状态时,它有可能重新执行。如通过对一个线程调用wait()函数后,线程就进入停滞状态,只有当两次对该线程调用notify或notifyAll后它才能两次回到可执行状态。
2 class Thread下的常用函数函数
21 suspend()、resume()
1) 通过suspend()函数,可使线程进入停滞状态。通过suspend()使线程进入停滞状态后,除非收到resume()消息,否则该线程不会变回可执行状态。
2) 当调用suspend()函数后,线程不会释放它的“锁标志”。
例11:
class TestThreadMethod extends Thread{
public static int shareVar = 0;
public TestThreadMethod(String name){
super(name);
}
public synchronized void run(){
if(shareVar==0){
for(int i=0; i<5; i++){
shareVar++;
if(shareVar==5){
thissuspend(); //(1)
}}}
else{
Systemoutprint(ThreadcurrentThread()getName());
Systemoutprintln(" shareVar = " + shareVar);
thisresume(); //(2)
}}
}
public class TestThread{
public static void main(String[] args){
TestThreadMethod t1 = new TestThreadMethod("t1");
TestThreadMethod t2 = new TestThreadMethod("t2");
t1start(); //(5)
//t1start(); //(3)
t2start(); //(4)
}}
运行结果为:
t2 shareVar = 5
i 当代码(5)的t1所产生的线程运行到代码(1)处时,该线程进入停滞状态。然后排程器从线程池中唤起代码(4)的t2所产生的线程,此时shareVar值不为0,所以执行else中的语句。
ii 也许你会问,那执行代码(2)后为什么不会使t1进入可执行状态呢?正如前面所说,t1和t2是两个不同对象的线程,而代码(1)和(2)都只对当前对象进行 *** 作,所以t1所产生的线程执行代码(1)的结果是对象t1的当前线程进入停滞状态;而t2所产生的线程执行代码(2)的结果是把对象t2中的所有处于停滞状态的线程调回到可执行状态。
iii 那现在把代码(4)注释掉,并去掉代码(3)的注释,是不是就能使t1重新回到可执行状态呢?运行结果是什么也不输出。为什么会这样呢?也许你会认为,当代码(5)所产生的线程执行到代码(1)时,它进入停滞状态;而代码(3)所产生的线程和代码(5)所产生的线程是属于同一个对象的,那么就当代码(3)所产生的线程执行到代码(2)时,就可使代码(5)所产生的线程执行回到可执行状态。但是要清楚,suspend()函数只是让当前线程进入停滞状态,但并不释放当前线程所获得的“锁标志”。所以当代码(5)所产生的线程进入停滞状态时,代码(3)所产生的线程仍不能启动,因为当前对象的“锁标志”仍被代码(5)所产生的线程占有。
#p#22 sleep()
1) sleep ()函数有一个参数,通过参数可使线程在指定的时间内进入停滞状态,当指定的时间过后,线程则自动进入可执行状态。
2) 当调用sleep ()函数后,线程不会释放它的“锁标志”。
例12:
class TestThreadMethod extends Thread{
class TestThreadMethod extends Thread{
public static int shareVar = 0;
public TestThreadMethod(String name){
super(name);
}
public synchronized void run(){
for(int i=0; i<3; i++){
Systemoutprint(ThreadcurrentThread()getName());
Systemoutprintln(" : " + i);
try{
Threadsleep(100); //(4)
}
catch(InterruptedException e){
Systemoutprintln("Interrupted");
}}}
}
public class TestThread{public static void main(String[] args){
TestThreadMethod t1 = new TestThreadMethod("t1");
TestThreadMethod t2 = new TestThreadMethod("t2");
t1start(); (1)
t1start(); (2)
//t2start(); (3)
}}
运行结果为:
t1 : 0
t1 : 1
t1 : 2
t1 : 0
t1 : 1
t1 : 2
由结果可证明,虽然在run()中执行了sleep(),但是它不会释放对象的“锁标志”,所以除非代码(1)的线程执行完run()函数并释放对象的“锁标志”,否则代码(2)的线程永远不会执行。
如果把代码(2)注释掉,并去掉代码(3)的注释,结果将变为:
t1 : 0
t2 : 0
t1 : 1
t2 : 1
t1 : 2
t2 : 2
由于t1和t2是两个对象的线程,所以当线程t1通过sleep()进入停滞时,排程器会从线程池中调用其它的可执行线程,从而t2线程被启动。
例13:
class TestThreadMethod extends Thread{
public static int shareVar = 0;
public TestThreadMethod(String name){
super(name);
}
public synchronized void run(){
for(int i=0; i<5; i++){
Systemoutprint(ThreadcurrentThread()getName());
Systemoutprintln(" : " + i);
try{
if(ThreadcurrentThread()getName()equals("t1"))
Threadsleep(200);
else
Threadsleep(100);
}
catch(InterruptedException e){
Systemoutprintln("Interrupted");
}}
}}
public class TestThread{public static void main(String[] args){
TestThreadMethod t1 = new TestThreadMethod("t1");
TestThreadMethod t2 = new TestThreadMethod("t2");
t1start();
//t1start();
t2start();
}}
运行结果为:
t1 : 0
t2 : 0
t2 : 1
t1 : 1
t2 : 2
t2 : 3
t1 : 2
t2 : 4
t1 : 3
t1 : 4
由于线程t1调用了sleep(200),而线程t2调用了sleep(100),所以线程t2处于停滞状态的时间是线程t1的一半,从从结果反映出来的就是线程t2打印两倍次线程t1才打印一次。
#p#23 yield()
1) 通过yield ()函数,可使线程进入可执行状态,排程器从可执行状态的线程中重新进行排程。所以调用了yield()的函数也有可能马上被执行。
2) 当调用yield ()函数后,线程不会释放它的“锁标志”。
例14:
class TestThreadMethod extends Thread{
public static int shareVar = 0;
public TestThreadMethod(String name){super(name);
}
public synchronized void run(){for(int i=0; i<4; i++){
Systemoutprint(ThreadcurrentThread()getName());
Systemoutprintln(" : " + i);
Threadyield();
}}
}
public class TestThread{public static void main(String[] args){
TestThreadMethod t1 = new TestThreadMethod("t1");
TestThreadMethod t2 = new TestThreadMethod("t2");
t1start();
t1start(); //(1)
//t2start(); (2)
}
}
运行结果为:
t1 : 0
t1 : 1
t1 : 2
t1 : 3
t1 : 0
t1 : 1
t1 : 2
t1 : 3
从结果可知调用yield()时并不会释放对象的“锁标志”。
如果把代码(1)注释掉,并去掉代码(2)的注释,结果为:
t1 : 0
t1 : 1
t2 : 0
t1 : 2
t2 : 1
t1 : 3
t2 : 2
t2 : 3
从结果可知,虽然t1线程调用了yield(),但它马上又被执行了。
24 sleep()和yield()的区别
1) sleep()使当前线程进入停滞状态,所以执行sleep()的线程在指定的时间内肯定不会执行;yield()只是使当前线程重新回到可执行状态,所以执行yield()的线程有可能在进入到可执行状态后马上又被执行。
2) sleep()可使优先级低的线程得到执行的机会,当然也可以让同优先级和高优先级的线程有执行的机会;yield()只能使同优先级的线程有执行的机会。
例15:
class TestThreadMethod extends Thread{
public static int shareVar = 0;
public TestThreadMethod(String name){
super(name);
}
public void run(){
for(int i=0; i<4; i++){
Systemoutprint(ThreadcurrentThread()getName());
Systemoutprintln(" : " + i);
//Threadyield(); (1)
/ (2) /
try{
Threadsleep(3000);
}
catch(InterruptedException e){
Systemoutprintln("Interrupted");
}}}
}
public class TestThread{
public static void main(String[] args){
TestThreadMethod t1 = new TestThreadMethod("t1");
TestThreadMethod t2 = new TestThreadMethod("t2");
t1setPriority(ThreadMAX_PRIORITY);
t2setPriority(ThreadMIN_PRIORITY);
t1start();
t2start();
}
}
运行结果为:
t1 : 0
t1 : 1
t2 : 0
t1 : 2
t2 : 1
t1 : 3
t2 : 2
t2 : 3
由结果可见,通过sleep()可使优先级较低的线程有执行的机会。注释掉代码(2),并去掉代码(1)的注释,结果为:
t1 : 0
t1 : 1
t1 : 2
t1 : 3
t2 : 0
t2 : 1
t2 : 2
t2 : 3
可见,调用yield(),不同优先级的线程永远不会得到执行机会。
25 join()
使调用join()的线程执行完毕后才能执行其它线程,在一定意义上,它可以实现同步的功能。
例16:
class TestThreadMethod extends Thread{
public static int shareVar = 0;
public TestThreadMethod(String name){
super(name);
}
public void run(){
for(int i=0; i<4; i++){
Systemoutprintln(" " + i);
try{
Threadsleep(3000);
}
catch(InterruptedException e){
Systemoutprintln("Interrupted");
}
}
}
}
public class TestThread{
public static void main(String[] args){
TestThreadMethod t1 = new TestThreadMethod("t1");
t1start();
try{
t1join();
}
catch(InterruptedException e){}
t1start();
}
}
运行结果为:
0
1
2
3
0
1
2
3
#p#3 class Object下常用的线程函数
wait()、notify()和notifyAll()这三个函数由javalangObject类提供,用于协调多个线程对共享数据的存取。
31 wait()、notify()和notifyAll()
1) wait()函数有两种形式:第一种形式接受一个毫秒值,用于在指定时间长度内暂停线程,使线程进入停滞状态。第二种形式为不带参数,代表waite()在notify()或notifyAll()之前会持续停滞。
2) 当对一个对象执行notify()时,会从线程等待池中移走该任意一个线程,并把它放到锁标志等待池中;当对一个对象执行notifyAll()时,会从线程等待池中移走所有该对象的所有线程,并把它们放到锁标志等待池中。
3) 当调用wait()后,线程会释放掉它所占有的“锁标志”,从而使线程所在对象中的其它synchronized数据可被别的线程使用。
例17:
下面,我们将对例11中的例子进行修改
class TestThreadMethod extends Thread{
public static int shareVar = 0;
public TestThreadMethod(String name){
super(name);
}
public synchronized void run(){
if(shareVar==0){
for(int i=0; i<10; i++){
shareVar++;
if(shareVar==5){
try{
thiswait(); //(4)
}
catch(InterruptedException e){}
}
}
}
if(shareVar!=0){
Systemoutprint(ThreadcurrentThread()getName());
Systemoutprintln(" shareVar = " + shareVar);
thisnotify(); //(5)
}
}
}
public class TestThread{
public static void main(String[] args){
TestThreadMethod t1 = new TestThreadMethod("t1");
TestThreadMethod t2 = new TestThreadMethod("t2");
t1start(); //(1)
//t1start(); (2)
t2start(); //(3)
}}
运行结果为:
t2 shareVar = 5
因为t1和t2是两个不同对象,所以线程t2调用代码(5)不能唤起线程t1。如果去掉代码(2)的注释,并注释掉代码(3),结果为:
t1 shareVar = 5
t1 shareVar = 10
这是因为,当代码(1)的线程执行到代码(4)时,它进入停滞状态,并释放对象的锁状态。接着,代码(2)的线程执行run(),由于此时shareVar值为5,所以执行打印语句并调用代码(5)使代码(1)的线程进入可执行状态,然后代码(2)的线程结束。当代码(1)的线程重新执行后,它接着执行for()循环一直到shareVar=10,然后打印shareVar。
#p#32 wait()、notify()和synchronized
waite()和notify()因为会对对象的“锁标志”进行 *** 作,所以它们必须在synchronized函数或synchronized block中进行调用。如果在non-synchronized函数或non-synchronized block中进行调用,虽然能编译通过,但在运行时会发生IllegalMonitorStateException的异常。
例18:
class TestThreadMethod extends Thread{
public int shareVar = 0;
public TestThreadMethod(String name){
super(name);
new Notifier(this);
}
public synchronized void run(){
if(shareVar==0){
for(int i=0; i<5; i++){
shareVar++;
Systemoutprintln("i = " + shareVar);
try{
Systemoutprintln("wait");
thiswait();
}
catch(InterruptedException e){}
}}
}
}
class Notifier extends Thread{
private TestThreadMethod ttm;
Notifier(TestThreadMethod t){
ttm = t;
start();
}
public void run(){
while(true){
try{
sleep(2000);
}
catch(InterruptedException e){}
/1 要同步的不是当前对象的做法 /
synchronized(ttm){
Systemoutprintln("notify");
ttmnotify();
}}
}
}
public class TestThread{
public static void main(String[] args){
TestThreadMethod t1 = new TestThreadMethod("t1");
t1start();
}
}
运行结果为:
i = 1
wait
notify
i = 2
wait
notify
i = 3
wait
notify
i = 4
wait
notify
i = 5
wait
notify
4 wait()、notify()、notifyAll()和suspend()、resume()、sleep()的讨论
41 这两组函数的区别
1) wait()使当前线程进入停滞状态时,还会释放当前线程所占有的“锁标志”,从而使线程对象中的synchronized资源可被对象中别的线程使用;而suspend()和sleep()使当前线程进入停滞状态时不会释放当前线程所占有的“锁标志”。
2) 前一组函数必须在synchronized函数或synchronized block中调用,否则在运行时会产生错误;而后一组函数可以non-synchronized函数和synchronized block中调用。
42 这两组函数的取舍
Java2已不建议使用后一组函数。因为在调用suspend()时不会释放当前线程所取得的“锁标志”,这样很容易造成“死锁”。
直接用socket和servsocket开发。命令行形式,改造成可视化的话,需要拆分功能。懒。
主要思路:
客户端 连接到服务 获取输入输出流。分别用2个线程处理输入流和输出流。
服务端 使用Set集合保存socket信息。同时使用线程安全Queue保存接收到的message。使用一个线程处理输入流,并将接到的封装成一个Message对象放到Queue中,对象保存连接的socket信息。另开一个线程用于从Queue获取消息,然后分发消息到Set中socket。
只是一个粗糙的即时聊天室。无缓冲功能。新用户加入进来不能获取以前的消息。只能获取加入后的其他人再发送的消息。
像这样:服务端测试:message中socket标识消息来源。
使用三个客户端测试:
Java Web 服务器的消息推送有以下几种方案:
1 轮询:前端使用ajax不停的发起请求获取想要的数据(最简单也是最容易耗尽服务器资源)。
2 长连接:>
以上就是关于java如何接收rabbitmq的base64消息全部的内容,包括:java如何接收rabbitmq的base64消息、java如何获取rabbitmq队列中消息数量、Java加密和数字签名 1消息摘要等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)