⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 helloworld.java

📁 jboss的jms一个小程序
💻 JAVA
字号:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import net.jxta.document.AdvertisementFactory;
import net.jxta.document.MimeMediaType;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.peergroup.PeerGroup;
import net.jxta.protocol.PipeAdvertisement;
import util.jxta.JxtaHelper;
import btpjxta.BTPPipeService;
import btpjxta.BTPService;
import btpjxta.BTPServiceFactory;
import btpjxta.Context;
import btpjxta.InputBTPPipe;
import btpjxta.MessageUtils;
import btpjxta.OutputBTPPipe;

public class HelloWorld {
	private InputBTPPipe inputPipe = null;
	private OutputBTPPipe outputPipe = null;
	private Context context = null;
	private JxtaHelper jxtaHelper = null;
	private PeerGroup netPG = null;
	protected BTPService btpSvc = null;
	private String inputPipeAdvFile = "C:\\peer1-pipe.xml"; // 输入管道的通告文件路径
	private PipeAdvertisement inputPipeAdv = null; // 本对等点的输入管道的通告
	private static boolean showTxMessages = true;
	protected Map outputPipeList = null;
	private Map outputPipeAdvList = null;

	public void startJxta() {
		jxtaHelper = new JxtaHelper(null, null);
		jxtaHelper.setJxtaEnv(JxtaHelper.DEFAULT_JXTA_HOME + "-" + "peer1"); // 设置系统的环境变量
																				// .jxta-[peername]

		if (!jxtaHelper.createCustomNetPeerGroup("helleworld", "helloword")) { // 创建自定义的点组
			System.out.println("创建点组失败");
		} else {
			System.out.println("创建点组成功");
		}

		netPG = jxtaHelper.startJxta(); // 启动JXTA平台
		if (netPG == null) {
			System.out.println("点组为空");
		} else {
			System.out.println("点组id :" + netPG.getPeerGroupID());
			System.out.println("点组名称 :" + netPG.getPeerGroupName());
			System.out.println("点组通告 :" + netPG.getPeerGroupAdvertisement());
			System.out.println("对等点ID :" + netPG.getPeerID());
			System.out.println("对等点名称 :" + netPG.getPeerName());
		}

		jxtaHelper.startRendezvous(); // 成为一个集合点

		try {
			BTPServiceFactory bsf = BTPServiceFactory.newInstance();
			this.btpSvc = bsf.getBTPService(jxtaHelper.getCurrentPeerGroup(),
					"peer1");
		} catch (Exception ex) {
			System.out.println("Cannot start BTPService" + ex);
		}
	}

	public OutputBTPPipe openOutputPipe() {
		PipeAdvertisement pipeAdv = null;
		try {
			FileInputStream is = new FileInputStream(inputPipeAdvFile);
			pipeAdv = (PipeAdvertisement) AdvertisementFactory
					.newAdvertisement(MimeMediaType.XMLUTF8, is);
			is.close();
		} catch (Exception e) {
			System.out.println("failed to read/parse pipe advertisement");
			e.printStackTrace();
			System.exit(-1);
		}
		BTPPipeService btpPipeSvc = btpSvc.getBTPPipeService();

		long timeout = 5000;
		OutputBTPPipe op = null;
		op = btpPipeSvc.createOutputBTPPipe(pipeAdv, timeout);

		if (op != null) {
			System.out.println("Output pipe '" + pipeAdv.getName()
					+ "' created");
			return op;
		} else {
			System.out.println("Failed to create the output pipe");
			return null;
		}
	}

	public void ctreatInputPipe() {
		BTPPipeService btpPipeSvc = btpSvc.getBTPPipeService();

		// load pipe advertisement from the file
		inputPipeAdv = (PipeAdvertisement) jxtaHelper.loadAdv(inputPipeAdvFile);
		if (inputPipeAdv == null) {
			System.out
					.println("Failed to load pipe advertisement from the file '"
							+ inputPipeAdvFile + "'");
		}

		System.out.println("Going to create an input pipe");

		// create new InputBTPPipe instance for given pipe advertisement
		inputPipe = btpPipeSvc.createInputBTPPipe(inputPipeAdv);
		if (inputPipe == null) {
			System.out.println("Failed to create the input pipe");
		}

		if (!jxtaHelper.publishPipeAdv(inputPipeAdv, 600000)) {
			System.out.println("Failed to publish the pipe advertisement");
		}

		System.out.println("Input pipe is created");
	}

	private boolean sendMessage(Message msg) {
		outputPipe = openOutputPipe();
		if (outputPipe == null)
			return false;
		if (msg == null)
			return false;

		try {
			if (outputPipe.send(msg)) {
				System.out.println("Message sent");
				return true;
			} else
				System.out.println("Failed to send a message");
		} catch (java.io.IOException ex) {
			System.out.println("Failed to send a message " + ex);
		}

		return false;
	}



	public static void main(String srgs[]) {
		HelloWorld hellow = new HelloWorld();
		hellow.startJxta();

		Message msg = new Message();
		StringMessageElement finishME = new StringMessageElement("finish",
				"finish", null);
		msg.addMessageElement(finishME);
		hellow.ctreatInputPipe();
		try {
			Thread.sleep(20000);
			if (hellow.sendMessage(msg)) {
				System.out.println("send suc"+ msg.getMessageElement("finish"));
			} else
				System.out.println("send fail");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -