pingclient java_Java Client.setPingInterval方法代碼示例

import org.apache.hadoop.ipc.Client; //導入方法依賴的package包/類

/**

* Runs the actual graph application through Hadoop Map-Reduce.

*

* @param verbose If true, provide verbose output, false otherwise

* @return True if success, false otherwise

* @throws ClassNotFoundException

* @throws InterruptedException

* @throws IOException

*/

public final boolean run(boolean verbose)

throws IOException, InterruptedException, ClassNotFoundException {

// Most users won't hit this hopefully and can set it higher if desired

setIntConfIfDefault("mapreduce.job.counters.limit", 5120);

// Capacity scheduler-specific settings. These should be enough for

// a reasonable Giraph job

setIntConfIfDefault("mapred.job.map.memory.mb", 1024);

setIntConfIfDefault("mapred.job.reduce.memory.mb", 0);

// Speculative execution doesn't make sense for Giraph

giraphConfiguration.setBoolean(

"mapred.map.tasks.speculative.execution", false);

// Set the ping interval to 5 minutes instead of one minute

// (DEFAULT_PING_INTERVAL)

Client.setPingInterval(giraphConfiguration, 60000 * 5);

// Should work in MAPREDUCE-1938 to let the user jars/classes

// get loaded first

giraphConfiguration.setBoolean("mapreduce.user.classpath.first", true);

giraphConfiguration.setBoolean("mapreduce.job.user.classpath.first", true);

// If the checkpoint frequency is 0 (no failure handling), set the max

// tasks attempts to be 0 to encourage faster failure of unrecoverable jobs

if (giraphConfiguration.getCheckpointFrequency() == 0) {

int oldMaxTaskAttempts = giraphConfiguration.getMaxTaskAttempts();

giraphConfiguration.setMaxTaskAttempts(0);

if (LOG.isInfoEnabled()) {

LOG.info("run: Since checkpointing is disabled (default), " +

"do not allow any task retries (setting " +

GiraphConstants.MAX_TASK_ATTEMPTS.getKey() + " = 0, " +

"old value = " + oldMaxTaskAttempts + ")");

}

}

// Set the job properties, check them, and submit the job

ImmutableClassesGiraphConfiguration conf =

new ImmutableClassesGiraphConfiguration(giraphConfiguration);

checkLocalJobRunnerConfiguration(conf);

//this part is just used for testing to delete the output dir.

File outputDir = new File(conf.get("mapred.work.output.dir")+"/partitions");

if(outputDir.exists()){

outputDir.delete();

}

Job submittedJob = new Job(conf, jobName);

if (submittedJob.getJar() == null) {

submittedJob.setJarByClass(getClass());

}

submittedJob.setNumReduceTasks(0);

submittedJob.setMapperClass(GraphMapper.class);

submittedJob.setInputFormatClass(BspInputFormat.class);

submittedJob.setOutputFormatClass(BspOutputFormat.class);

GiraphJobObserver jobObserver = conf.getJobObserver();

jobObserver.launchingJob(submittedJob);

boolean passed = submittedJob.waitForCompletion(verbose);

jobObserver.jobFinished(submittedJob, passed);

return passed;

}

以下是基于UDP实现ping命令的Java代码,包括服务器端pingServer功能和客户端pingClient功能。 pingServer代码如下: ```java import java.net.*; public class PingServer { private static final int PORT = 12345; public static void main(String[] args) throws Exception { DatagramSocket serverSocket = new DatagramSocket(PORT); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while (true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); InetAddress clientAddress = receivePacket.getAddress(); int clientPort = receivePacket.getPort(); String message = new String(receivePacket.getData(), 0, receivePacket.getLength()); System.out.println("Received message: " + message); String response = "Pong"; sendData = response.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientAddress, clientPort); serverSocket.send(sendPacket); } } } ``` pingClient代码如下: ```java import java.net.*; public class PingClient { private static final int PORT = 12345; public static void main(String[] args) throws Exception { DatagramSocket clientSocket = new DatagramSocket(); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; InetAddress serverAddress = InetAddress.getByName("localhost"); for (int i = 0; i < 10; i++) { String message = "Ping"; sendData = message.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, PORT); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String response = new String(receivePacket.getData(), 0, receivePacket.getLength()); System.out.println("Received response: " + response); Thread.sleep(1000); } clientSocket.close(); } } ``` 运行pingServer和pingClient,可以看到客户端发送了10个Ping消息,服务器端收到后返回Pong消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值