首页 文章 精选 留言 我的

精选列表

搜索[微服务架构],共10000篇文章
优秀的个人博客,低调大师

《RocketMQ技术内幕:RocketMQ架构设计与实现原理》—1.1.1 Eclipse获取RocketMQ源码

1.1 获取和调试RocketMQ的源代码 RocketMQ原先是阿里巴巴内部使用的消息中间件,于2017年提交到Apache基金会成为Apache基金会的顶级开源项目,GitHub代码库链接:https://github.com/apache/rocketmq.git 在Github网站上搜索RocketMQ,如图1-1所示。 1.1.1 Eclipse获取RocketMQ源码Step1:单击右键从菜单中选择import git,弹出如图1-2所示的对话框。Step2:点击Next按钮,弹出Projects from Git对话框,如图1-3所示。Step3:点击Next按钮,弹出Clone URI对话框,如图1-4所示。Step4:继续点击Next进入下一步,选择代码分支,如图1-5所示Step5:选择所需要的分支后点击Next, 本文为云栖社区原创内容,未经允许不得转载,如需转载请发送邮件至yqeditor@list.alibaba-inc.com;如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:yqgroup@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。 网友评论 登录后评论 0/500

优秀的个人博客,低调大师

【Android架构】基于MVP模式的Retrofit2+RXjava封装之文件下载(二)

上篇中我们介绍了基于MVP的Retrofit2+RXjava封装,还没有看的点击这里,这一篇我们来说说文件下载的实现。 首先,我们先在ApiServer定义好调用的接口 @GET Observable<ResponseBody> downloadFile(@Url String fileUrl); 接着定义一个接口,下载成功后用来回调 public interface FileView extends BaseView { void onSuccess(File file); } 接着是Observer,建议与处理普通接口的Observer区分处理 public abstract class FileObsever extends BaseObserver<ResponseBody> { private String path; public FileObsever(BaseView view, String path) { super(view); this.path = path; } @Override protected void onStart() { } @Override public void onComplete() { } @Override public void onSuccess(ResponseBody o) { } @Override public void onError(String msg) { } @Override public void onNext(ResponseBody o) { File file = FileUtil.saveFile(path, o); if (file != null && file.exists()) { onSuccess(file); } else { onErrorMsg("file is null or file not exists"); } } @Override public void onError(Throwable e) { onErrorMsg(e.toString()); } public abstract void onSuccess(File file); public abstract void onErrorMsg(String msg); } FileUtil 注:如果需要写入文件的进度,可以在将这段方法放在onNext中,在FileObsever这个类写个方法,然后回调。 public static File saveFile(String filePath, ResponseBody body) { InputStream inputStream = null; OutputStream outputStream = null; File file = null; try { if (filePath == null) { return null; } file = new File(filePath); if (file == null || !file.exists()) { file.createNewFile(); } long fileSize = body.contentLength(); long fileSizeDownloaded = 0; byte[] fileReader = new byte[4096]; inputStream = body.byteStream(); outputStream = new FileOutputStream(file); while (true) { int read = inputStream.read(fileReader); if (read == -1) { break; } outputStream.write(fileReader, 0, read); fileSizeDownloaded += read; } outputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return file; } 下来是FilePresenter public class FilePresenter extends BasePresenter<FileView> { public FilePresenter(FileView baseView) { super(baseView); } public void downFile(String url, final String path) { addDisposable(apiServer.downloadFile(url), new FileObsever(baseView, path) { @Override public void onSuccess(File file) { if (file != null && file.exists()) { baseView.onSuccess(file); } else { baseView.showError("file is null"); } } @Override public void onErrorMsg(String msg) { baseView.showError(msg); } }); } } 最后在Activity中调用 private void downFile() { String url = "http://download.sdk.mob.com/apkbus.apk"; String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) {// 检查是否有存储卡 dir = Environment.getExternalStorageDirectory() + "/ceshi/"; File dirFile = new File(dir); if (!dirFile.exists()) { dirFile.mkdirs(); } } presenter.downFile(url, dir + "app-debug.apk"); } 就在我以为万事大吉的时候,APP崩溃了,错误信息如下: [图片上传失败...(image-39e2a7-1532052138950)] 原来是加入日志监听器,会导致每次都把整个文件加载到内存,那我们就去掉这个 修改FilePresenter#downFile如下: public void downFile(String url, final String path) { OkHttpClient client = new OkHttpClient.Builder().build(); Retrofit retrofit = new Retrofit.Builder().client(client) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .baseUrl("https://wawa-api.vchangyi.com/").build(); apiServer = retrofit.create(ApiServer.class); addDisposable(apiServer.downloadFile(url), new FileObsever(baseView, path) { @Override public void onSuccess(File file) { if (file != null && file.exists()) { baseView.onSuccess(file); } else { baseView.showError("file is null"); } } @Override public void onErrorMsg(String msg) { baseView.showError(msg); } }); } 这次倒是下载成功了,不过官方建议10M以上的文件用Streaming标签,我们加上Streaming标签试试 修改ApiServer @Streaming @GET /** * 大文件官方建议用 @Streaming 来进行注解,不然会出现IO异常,小文件可以忽略不注入 */ Observable<ResponseBody> downloadFile(@Url String fileUrl); 这次又崩溃了,错误信息如下: [图片上传失败...(image-fc8c28-1532052138951)] 这是怎么回事,我们网络请求是在子线程啊。无奈之下只得翻翻官方文档,原来使用该注解表示响应用字节流的形式返回.如果没使用该注解,默认会把数据全部载入到内存中。我们可以在主线程中处理写入文件(不建议),但不能在主线程中处理字节流。所以,我们需要将处理字节流、写入文件都放在子线程中。 于是,修改FilePresenter#downFile如下: OkHttpClient client = new OkHttpClient.Builder().build(); Retrofit retrofit = new Retrofit.Builder().client(client) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .baseUrl("https://wawa-api.vchangyi.com/").build(); apiServer = retrofit.create(ApiServer.class); apiServer .downloadFile(url) .map(new Function<ResponseBody, String>() { @Override public String apply(ResponseBody body) throws Exception { File file = FileUtil.saveFile(path, body); return file.getPath(); } }).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new FileObserver(baseView) { @Override public void onSuccess(File file) { baseView.onSuccess(file); } @Override public void onError(String msg) { baseView.showError(msg); } }); 这样,下载文件算是完成了,好像还缺点什么?对,缺个下载进度,还记得拦截器吗,我们可以从这里入手: public class ProgressResponseBody extends ResponseBody { private ResponseBody responseBody; private BufferedSource bufferedSource; private ProgressListener progressListener; public ProgressResponseBody(ResponseBody responseBody, ProgressListener progressListener) { this.responseBody = responseBody; this.progressListener = progressListener; } @Nullable @Override public MediaType contentType() { return responseBody.contentType(); } @Override public long contentLength() { return responseBody.contentLength(); } @Override public BufferedSource source() { if (bufferedSource == null) { bufferedSource = Okio.buffer(source(responseBody.source())); } return bufferedSource; } private Source source(Source source) { return new ForwardingSource(source) { long totalBytesRead = 0L; @Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead = super.read(sink, byteCount); totalBytesRead += bytesRead; progressListener.onProgress(responseBody.contentLength(), totalBytesRead); return bytesRead; } }; } public interface ProgressListener { void onProgress(long totalSize, long downSize); } } 在BaseView 中定义接口,个人建议放在BaseView 中,在BaseActivity中实现BaseView,方便复用 /** * 下载进度 * * @param totalSize * @param downSize */ void onProgress(long totalSize, long downSize); 再次修改FilePresenter#downFile如下: public void downFile(final String url, final String path) { OkHttpClient client = new OkHttpClient.Builder() .addNetworkInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Response response = chain.proceed(chain.request()); return response.newBuilder().body(new ProgressResponseBody(response.body(), new ProgressResponseBody.ProgressListener() { @Override public void onProgress(long totalSize, long downSize) { baseView.onProgress(totalSize, downSize); } })).build(); } }).build(); Retrofit retrofit = new Retrofit.Builder().client(client) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .baseUrl("https://wawa-api.vchangyi.com/").build(); apiServer = retrofit.create(ApiServer.class); apiServer .downloadFile(url) .map(new Function<ResponseBody, String>() { @Override public String apply(ResponseBody body) throws Exception { File file = FileUtil.saveFile(path, body); return file.getPath(); } }).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new FileObserver(baseView) { @Override public void onSuccess(File file) { baseView.onSuccess(file); } @Override public void onError(String msg) { baseView.showError(msg); } }); } 至此,使用Retrofit下载文件暂时告一段落。 你的认可,是我坚持更新博客的动力,如果觉得有用,就请点个赞,谢谢 项目源码

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册