首页 > 基础设施 > 正文

创建执行计算密集型任务的Java应用程序

2012-10-29 14:05:21  来源:中云

摘要:通过Windows Azure,你可以使用一个虚拟机来处理计算密集型任务,本文将给您介绍一下如何创建一个执行计算密集型任务的Java应用程序。
关键词: Windows Az

    通过Windows Azure,你可以使用一个虚拟机来处理计算密集型任务。前面给大家介绍了云平台上创建一个虚拟机云平台上创建一个服务总线命名空间,本文将给您介绍一下如何创建一个执行计算密集型任务的Java应用程序


    1. 在你的开发机器(并不一定是你所创建的虚拟机)上,为Java下载Windows Azure SDK.


    2. 在本小节的末尾创建一个Java控制台应用程序使用的示例代码。在本教程中,我们将使用TSPSolver.Java作为Java文件的名字。修改your_service_bus_namespace、 your_service_bus_owner、 以及 your_service_bus_key 等占位符,分别使用您的服务总线命名空间、 默认发行人和默认密钥值。


    3. 编码后,导出应用程序到一个可运行的Java归档(JAR),并把所需的库到打包已生成的JAR.在本教程中,我们将使用TSPSolver.jar作为生成的JAR名称。


    // TSPSolver.Java


    import com.microsoft.WindowsAzure.services.core.Configuration;


    import com.microsoft.WindowsAzure.services.core.ServiceException;


    import com.microsoft.WindowsAzure.services.serviceBus.*;


    import com.microsoft.WindowsAzure.services.serviceBus.models.*;


    import Java.io.*;


    import Java.text.DateFormat;


    import Java.text.SimpleDateFormat;


    import Java.util.ArrayList;


    import Java.util.Date;


    import Java.util.List;


    public class TSPSolver {


    //  Value specifying how often to provide an update to the console.


    private static long loopCheck = 100000000;


    private static long nTimes = 0, nLoops=0;


    private static double[][] distances;


    private static String[] cityNames;


    private static int[] bestOrder;


    private static double minDistance;


    private static ServiceBusContract service;


    private static void buildDistances(String fileLocation, int numCities) throws Exception{


    try{


    BufferedReader file = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(new File(fileLocation)))));


    double[][] cityLocs = new double[numCities][2];


    for (int i = 0; i<numCities; i++){


    String[] line = file.readLine()。split(“, ”);


    cityNames[i] = line[0];


    cityLocs[i][0] = Double.parseDouble(line[1]);


    cityLocs[i][1] = Double.parseDouble(line[2]);


    }


    for (int i = 0; i<numCities; i++){


    for (int j = i; j<numCities; j++){


    distances[i][j] = Math.hypot(Math.abs(cityLocs[i][0] - cityLocs[j][0]), Math.abs(cityLocs[i][1] - cityLocs[j][1]));


    distances[j][i] = distances[i][j];


    }


    }


    } catch (Exception e){


    throw e;


    }


    }


    private static void permutation(List<Integer> startCities, double distSoFar, List<Integer> restCities) throws Exception {


    try


    {


    nTimes++;


    if (nTimes == loopCheck)


    {


    nLoops++;


    nTimes = 0;


    DateFormat dateFormat = new SimpleDateFormat(“MM/dd/yyyy HH:mm:ss”);


    Date date = new Date();


    System.out.print(“Current time is ” + dateFormat.format(date) + “. ”);


    System.out.println(  “Completed ” + nLoops + “ iterations of size of ” + loopCheck + “.”);


    }


    if ((restCities.size() == 1) && ((minDistance == -1) || (distSoFar + distances[restCities.get(0)][startCities.get(0)] + distances[restCities.get(0)][startCities.get(startCities.size()-1)] < minDistance))){


    startCities.add(restCities.get(0));


    newBestDistance(startCities, distSoFar + distances[restCities.get(0)][startCities.get(0)] + distances[restCities.get(0)][startCities.get(startCities.size()-2)]);


    startCities.remove(startCities.size()-1);


    }


    else{


    for (int i=0; i<restCities.size(); i++){


    startCities.add(restCities.get(0));


    restCities.remove(0);


    permutation(startCities, distSoFar + distances[startCities.get(startCities.size()-1)][startCities.get(startCities.size()-2)],restCities);


    restCities.add(startCities.get(startCities.size()-1));


    startCities.remove(startCities.size()-1);


    }


    }


    }


    catch (Exception e)


    {


    throw e;


    }


    }


    private static void newBestDistance(List<Integer> cities, double distance) throws ServiceException, Exception {


    try


    {


    minDistance = distance;


    String cityList = “Shortest distance is ”+minDistance+“, with route: ”;


    for (int i = 0; i<bestOrder.length; i++){


    bestOrder[i] = cities.get(i);


    cityList += cityNames[bestOrder[i]];


    if (i != bestOrder.length -1)


    cityList += “, ”;


    }


    System.out.println(cityList);


    service.sendQueueMessage(“TSPQueue”, new BrokeredMessage(cityList));


    }


    catch (ServiceException se)


    {


    throw se;


    }


    catch (Exception e)


    {


    throw e;


    }


    }


    public static void main(String args[]){


    try {


    Configuration config = ServiceBusConfiguration.configureWithWrapAuthentication(


    “your_service_bus_namespace”, “your_service_bus_owner”, “your_service_bus_key”);


    service = ServiceBusService.create(config);


    int numCities = 10;  // Use as the default, if no value is specified at command line.


    if (args.length != 0)


    {


    if (args[0].toLowerCase()。compareTo(“createqueue”)==0)


    {


    // No processing to occur other than creating the queue.


    QueueInfo queueInfo = new QueueInfo(“TSPQueue”);


    service.createQueue(queueInfo);


    System.out.println(“Queue named TSPQueue was created.”);


    System.exit(0);


    }


    if (args[0].toLowerCase()。compareTo(“deletequeue”)==0)


    {


    // No processing to occur other than deleting the queue.


    service.deleteQueue(“TSPQueue”);


    System.out.println(“Queue named TSPQueue was deleted.”);


    System.exit(0);


    }


    // Neither creating or deleting a queue.


    // Assume the value passed in is the number of cities to solve.


    numCities = Integer.valueOf(args[0]);


    }


    System.out.println(“Running for ” + numCities + “ cities.”);


    List<Integer> startCities = new ArrayList<Integer>();


    List<Integer> restCities = new ArrayList<Integer>();


    startCities.add(0);


    for(int i = 1; i<numCities; i++)


    restCities.add(i);


    distances = new double[numCities][numCities];


    cityNames = new String[numCities];


    buildDistances(“c:\TSP\cities.txt”, numCities);


    minDistance = -1;


    bestOrder = new int[numCities];


    permutation(startCities, 0, restCities);


    System.out.println(“Final solution found!”);


    service.sendQueueMessage(“TSPQueue”, new BrokeredMessage(“Complete”));


    }


    catch (ServiceException se)


    {


    System.out.println(se.getMessage());


    se.printStackTrace();


    System.exit(-1);


    }


    catch (Exception e)


    {


    System.out.println(e.getMessage());


    e.printStackTrace();


    System.exit(-1);


    }


    }


    }




【相关阅读】

创建监视计算密集型任务进展的Java应用程序

利用Windows Azure创建服务总线命名空间

利用Windows Azure在云平台上创建虚拟机
 


第三十四届CIO班招生
国际CIO认证培训
首席数据官(CDO)认证培训
责编:zhangyexi

免责声明:本网站(http://www.ciotimes.com/)内容主要来自原创、合作媒体供稿和第三方投稿,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证有关资料的准确性及可靠性,读者在使用前请进一步核实,并对任何自主决定的行为负责。本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。